C++ 如何使用result_代替decltype?
在中,我创建了一个类型特征:C++ 如何使用result_代替decltype?,c++,typetraits,overloading,decltype,result-of,C++,Typetraits,Overloading,Decltype,Result Of,在中,我创建了一个类型特征: template<typename T> using to_string_t = decltype(to_string(declval<T>())); 但我得到一个编译器错误,大致如下: 错误C2275:“T”:非法使用此类型作为表达式 注:见“T”的声明 错误C2974:'std::result_of':应为'\u Fty'类型的模板参数无效 我尝试了其他几个输入到的result\u,但都没有成功,有人能帮我理解的result\u的参数是
template<typename T>
using to_string_t = decltype(to_string(declval<T>()));
但我得到一个编译器错误,大致如下:
错误C2275:“T”:非法使用此类型作为表达式注:见“T”的声明
错误C2974:'std::result_of':应为'\u Fty'类型的模板参数无效
我尝试了其他几个输入到的
result\u,但都没有成功,有人能帮我理解
的result\u的参数是什么吗?让我们修补一下std::result_of
只需要类型,其结果应该从其type
内部typedef中检索,并且您需要typename
来访问所述typedef,因为它依赖于模板参数
template<typename T>
using to_string_t = typename std::result_of<decltype(std::to_string)(T)>::type;
^^^^^^^^ ^^^^^^^^ ^^^^^^
好吧
main.cpp:5:68: error: decltype cannot resolve address of overloaded function
对,std::to_string
是重载的,所以我们需要通过将其转换为其重载之一来消除歧义
template<typename T>
using to_string_t = typename std::result_of<decltype(static_cast<
模板
使用to_string\u t=typename std::result\u ofdecltype(decltype)(std::to_string(declval())
:)@bolov缺少右括号,但即使这样,您仍将调用std::string
作为函数。此外,这几乎无法避免decltype
/declval
:psry:这里是:std::result\u of(现在已经测试过了,sry关于这一点)。但是,是的,它使用decltype来获取到字符串的结果,以便创建一个在result\u of中使用的类型来获取到字符串的结果。但是…它使用到字符串的结果:)@bolov不能真的反驳这个…:p@JonathanMee检索
的result\u所需函数类型的唯一方法是解决重载问题。我们可以通过强制转换来手动执行此操作(但随后我们提供了我们正在搜索的信息),或者使用decltype
在其未评估的上下文中执行此操作。也就是说,我们回到了原点1。
main.cpp:5:68: error: decltype cannot resolve address of overloaded function
template<typename T>
using to_string_t = typename std::result_of<decltype(static_cast<