考虑以下程序:
extern int x;
auto x = 42;
int main() { }
Run Code Online (Sandbox Code Playgroud)
Clang 3.5接受它(现场演示),GCC 4.9和VS2013不接受(前者的现场演示).谁是对的,C++标准中指定的正确行为在哪里?
标准是否允许以下内容?
#include <iostream>
extern int a;
auto a = 3;
int main(int, char**)
{
std::cout << a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要使用复杂(许多模板参数)类型定义静态成员(不是constexpr).因此,希望有这样的东西:
struct X {
static auto x = makeObjectWithComplexType();
};
Run Code Online (Sandbox Code Playgroud)
但它不是C++.所以我试着解决它,并认为下面的代码片段可以工作,但它没有:
#include <string>
struct X {
static auto abc() {
return std::string();
}
static decltype(abc()) x;
};
decltype(abc()) X::x;
int main() {}
Run Code Online (Sandbox Code Playgroud)
它失败并出现错误:错误:在扣除'auto`之前使用'static auto X :: abc()'
有没有办法让上面的代码段工作.或者有没有其他方法来定义具有推导类型的静态成员?