【发布时间】:2015-12-24 19:41:56
【问题描述】:
以下代码适用于 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 编译?谢谢。 p.s.我显然不需要template <typename T>,但这只是为了说明。
【问题讨论】:
-
用 gcc 5.2 Demo987654321@进行编译
-
@Jarod42 啊谢谢我去下载最新的gcc试试。
标签: c++ templates c++14 clang++ g++4.9