not*_*ser 21 c++ scope overloading default-value
示例:这是合法的C++ 14吗?
#include <iostream>
static int d() {
return 42;
}
static int e(int d = d()) {
return d;
}
int main() {
std::cout << e() << " " << e(-1) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
g ++ 5.4 -std=c++14喜欢它,但clang ++ 3.8 -std=c++14抱怨:
samename.cxx:3:23: error: called object type 'int' is not a function or function pointer
static int e(int d = d()) {return d;}
~^
Run Code Online (Sandbox Code Playgroud)
Whi*_*TiM 16
声明的声明就在其完整的声明者之后和初始化者之前(如果有的话),除非如下所述.
例:
Run Code Online (Sandbox Code Playgroud)unsigned char x = 12; { unsigned char x = x; }这里第二个
x用自己的(不确定的)值初始化.
Clang与标准的这一部分一致.海湾合作委员会似乎只适用于它{block}.
鉴于:
constexpr int d() {return 42;}
Run Code Online (Sandbox Code Playgroud)
以下在Clang中失败,但在GCC中有效:
static int e(int d = d()) {return d;}
// This ^^d should refer to the new name that shadows ::d()
Run Code Online (Sandbox Code Playgroud)
以下在Clang和GCC中都失败了:
void func(){
int d = d();
// ^^d here refers to the new name that shadowed ::d();
}
Run Code Online (Sandbox Code Playgroud)
cbu*_*art 11
它似乎不合法:参见C++ 14规范(第3.3.2节 - 声明点).
int x = 5;
{ int x = x; } // second x is initialized with its own undetermined state
{ int x[x]; } // declares an array with 5 elements, since the declaration of
// second x is not complete when the first x is used
Run Code Online (Sandbox Code Playgroud)
在你的情况下,声明d是完整的,所以当你使用d()你指的是变量,而不是函数.