【问题标题】:Template declaration of decltype with g++使用 g++ 的 decltype 模板声明
【发布时间】: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 &lt;typename T&gt;,但这只是为了说明。

【问题讨论】:

  • 用 gcc 5.2 Demo987654321@进行编译
  • @Jarod42 啊谢谢我去下载最新的gcc试试。

标签: c++ templates c++14 clang++ g++4.9


【解决方案1】:

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

【讨论】:

  • 好的,谢谢。根据 Jarod42,它似乎在 gcc 5.2 中工作。几分钟后会接受这个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多