gpt4 book ai didi

c++ - 使用 g++ 的 decltype 模板声明

转载 作者:行者123 更新时间:2023-11-30 02:36:25 26 4
gpt4 key购买 nike

以下代码适用于 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 编译代码?谢谢。附:我显然不需要 template <typename T>但这只是为了说明。

最佳答案

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

关于c++ - 使用 g++ 的 decltype 模板声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32808178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com