【发布时间】:2015-01-18 22:00:40
【问题描述】:
首先,我有这样的东西,一个重命名的 function_traits 来获取 lambda 的返回类型
template <typename T>
struct FuncAnalyzer
{
};
template <typename T, typename TRet, typename... TArgs>
struct FuncAnalyzer<TRet(T::*)(TArgs...) const>
{
using TReturn = TRet;
};
template <typename T>
struct FunctionAnalyzer
: public FuncAnalyzer<decltype(&T::operator())>
{
};
那么当我在一个方法中有这个时,compi:
auto a = [](const int& key) -> QString { return QString::number(key); };
using b = FunctionAnalyzer<decltype(a)>::TReturn;
b x;
但是当我尝试将它放入 lambda 时,它不起作用
auto c = [](const int& key) -> QString
{
auto a = [](const int& key) -> QString { return QString::number(key); };
using b = FunctionAnalyzer<decltype(a)>::TReturn;
b x;
return QString::number(key);
};
编译输出:
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2825: 'T': must be a class or namespace when followed by '::'
1> Controller\Schema\SchemaController.cpp(105) : see reference to class template instantiation 'ValidSig::FunctionAnalyzer<QString (__cdecl *)(const int &)>' being compiled
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2039: '()' : is not a member of '`global namespace''
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2275: 'T' : illegal use of this type as an expression
1> Controller\Schema\SchemaController.cpp(105) : see declaration of 'T'
1> Controller\Schema\SchemaController.cpp(105) : see declaration of 'T'
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2146: syntax error : missing ')' before identifier '()'
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2143: syntax error : missing ',' before ')'
1>i:\uicgraph\common\FunctionAnalyzer.h(21): error C2947: expecting '>' to terminate template-argument-list, found '>'
1>Controller\Schema\SchemaController.cpp(105): error C2039: 'TReturn' : is not a member of 'ValidSig::FunctionAnalyzer<QString (__cdecl *)(const int &)>'
1>Controller\Schema\SchemaController.cpp(105): error C2061: syntax error : identifier 'TReturn'
我正在使用 MSVC 2013
【问题讨论】:
-
我试过你的代码(把QString改成int,因为我没有安装Qt),它似乎在g++和clang++中工作,见rextester.com/JZOU33170这似乎是一个VC问题,因为每当您将编译器更改为 VC++ 时都会出现错误
标签: c++ templates visual-c++ lambda