【发布时间】:2017-02-16 09:27:03
【问题描述】:
我正在学习如何获取重载函数test() 与test(double) 的返回值的type。
我从a SO answer (by chris) 修改了代码。
#include <type_traits>
#include <utility>
int test();
double test(double x);
template<typename... Ts>
using TestType = decltype(test(std::declval<Ts>()...))(Ts...);
int main() {
std::result_of< TestType<double> >::type n = 0;
//^ ### compile error ###
using doubleDat = std::result_of< TestType<double> >::type ;
doubleDat n=0;
}
我得到一个编译错误。
错误:“std::result_of”中没有名为“type”的类型
据我了解:-
TestType<...>是“可变参数模板”。
用我自己的话来说,它就像一个压缩的缩写,有任意数量的参数。TestType<double>是test(double)函数的id。-
std::result_of<TestType<double>>::type是test(double)的返回类型。doubleDat应该是double。
问题:为什么不能编译?如何解决?
我已经阅读了这些:-
-
decltype for overloaded member function - 没有关于
std::result_of的例子 -
c++: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map - 与线程相关的问题,而不是
std::result_of
线索:经过长时间的搜索,我隐隐约约感觉到我的代码遭受了“最烦人的解析”。
【问题讨论】:
标签: c++11 variadic-templates overloading decltype result-of