And*_*ard 5 c++ gcc clang c++11 c++14
以下代码在使用 -std=c++11 的 GCC 6.3 中可以正常编译。
template <typename T>
struct MetaFunction
{
static int value;
};
#define MY_MACRO(T) (MetaFunction<T>::value)
template <class T, const int* p = &MY_MACRO(T)>
struct Foo
{
void DoSomething()
{
}
};
int main()
{
Foo< int > f;
f.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
但如果指定 -std=c++14 则会发出以下错误:
main.cpp:19:14: error: '& MetaFunction<int>::value' is not a valid template argument for 'const int*' because it is not the address of a variable
Foo< int > f;
^
Run Code Online (Sandbox Code Playgroud)
无论指定 -std=c++11 还是 -std=c++14,Clang 3.8 都会给出类似的错误。
main.cpp:11:36: error: non-type template argument does not refer to any declaration
template <class T, const int* p = &MY_MACRO(T)>
^~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
MY_MACRO(T)从更改为(MetaFunction<T>::value)可以MetaFunction<T>::value解决问题。
这里哪个编译器是正确的?C++14 标准是否包含使 GCC 行为符合预期的更改?或者 Clang 在 C++11 下发出错误也是正确的吗?
| 归档时间: |
|
| 查看次数: |
238 次 |
| 最近记录: |