1

以下代码适用于 clang 版本 3.6.0。但是当我将它与 g++ 4.9.2 (Ubuntu 4.9.2-10ubuntu13) 一起使用时,我得到一个错误:

//g++ -std=c++14 testgcc.cpp

#include <iostream>
using namespace std;

template<typename T>
constexpr auto doSomething(){
  return 123;
}

template<typename T>
decltype(doSomething<T>()) result = doSomething<T>();

decltype(doSomething<int>()) result2 = result<int>;

int main(void){
  cout<<result2<<endl;
}

我得到的错误是:

testgcc.cpp:12:28: error: template declaration of ‘decltype (doSomething<T>()) result’
 decltype(doSomething<T>()) result = doSomething<T>();
                            ^
testgcc.cpp:14:40: error: ‘result’ was not declared in this scope
 decltype(doSomething<int>()) result2 = result<int>;
                                        ^
testgcc.cpp:14:47: error: expected primary-expression before ‘int’
 decltype(doSomething<int>()) result2 = result<int>;

有没有办法让代码用 gcc 编译?谢谢。ps 我显然不需要,template <typename T>但这只是为了说明。

4

1 回答 1

3

根据GCC 中的 C++1y/C++14 Support , g++ 4.9.2 不支持变量模板。

于 2015-09-27T13:10:34.647 回答