C++ std::内置运算符的结果
通过的C++ std::内置运算符的结果,c++,c++11,typetraits,C++,C++11,Typetraits,通过的result\u确定-int()或double()*double()之类的结果的正确语法是什么 这失败了 std::result_of<operator-(int)>::type std::result_of<operator*(double,double)>::type std::result\u of::type std::result\u of::type std::result\u的确实不是这里要采用的方法decltype完全满足您的需要,可以用作decl
result\u确定-int()
或double()*double()
之类的结果的正确语法是什么
这失败了
std::result_of<operator-(int)>::type
std::result_of<operator*(double,double)>::type
std::result\u of::type
std::result\u of::type
std::result\u的
确实不是这里要采用的方法decltype
完全满足您的需要,可以用作decltype(-int())
,decltype(double()*double())
等。如果您不知道该类型是否可以默认构造,也可以使用std::declval
:decltype(-std::declval())
任何涉及运算符-
的语法都不起作用的原因是运算符-
语法只适用于自定义重载运算符。内置运算符没有任何可引用的支持函数。decltype
无疑是解决此问题的方法,但如果必须使用的结果,则可以使用
中定义的
例如,要获得结果类型的double*double
,请使用
std::result_of<std::multiplies<double>(double, double)>::type
std::result\u of::type
同样地,一元否定也可以是
std::result_of<std::negate<int>(int)>::type
std::result\u of::type
使用C++14,您甚至可以在两种不同类型上查询数学运算的结果类型
std::result_of<std::plus<>(double, int)>::type
std::result\u of::type
当然,同样的技术也可以用于用户定义的类型
struct foo{};
struct bar{};
bar operator/(foo, foo) { return bar{}; }
std::result_of<std::divides<>(foo, foo)>::type
structfoo{};
结构条{};
条运算符/(foo,foo){返回条{};}
std::result\u of::type
您可以改用decltype
。@b4handtypename
仅当传递给模板的参数依赖于模板参数时才是必需的int
和double
不相关。