BOOST_TYPEOF返回int const int的,而不是 [英] BOOST_TYPEOF returns int instead of const int
问题描述
能否请您解释一下为什么这个版画 1
?不应该 BOOST_TYPEOF
收益 const int的
。如何可以检查函数返回常量
不使用C ++ 11的功能?
的#include<&iostream的GT;#包括LT&;提升/ typeof运算/ typeof.hpp>
#包括LT&;升压/ type_traits / is_same.hpp>const int的f_const_int(){返回1;}诠释的main()
{
的typedef BOOST_TYPEOF(f_const_int())型;
性病::法院LT&;< (::提振&is_same LT;类型,INT> ::值)LT;<的std :: ENDL;
}
如果一个prvalue前pression已输入的 CV 的 INT
,即CV限定符被忽略。 [EXPR] / 6:
如果一个prvalue最初有型的 CV 的
T
,其中T
是
CV-不合格的非类,非数组类型,前pression的类型
在进一步分析之前调整到T
。
块引用>宏也因此永远不会收到该信息的返回类型为
常量
。结果
可能的解决方法:的#include<升压/ type_traits / function_traits.hpp>// [...]
TYPEDEF提振:: function_traits< BOOST_TYPEOF(f_const_int)GT; :: result_type的类型;演示 。
Can you please explain why this prints
1
? Should notBOOST_TYPEOF
returnconst int
. How one can check if function returnsconst
without using c++11 features?#include <iostream> #include <boost/typeof/typeof.hpp> #include <boost/type_traits/is_same.hpp> const int f_const_int() {return 1;} int main() { typedef BOOST_TYPEOF(f_const_int()) type; std::cout << (boost::is_same<type, int>::value) << std::endl; }
解决方案If a prvalue expression has type cv
int
, that cv-qualifier is ignored. [expr]/6:If a prvalue initially has the type "cv
T
," whereT
is a cv-unqualified non-class, non-array type, the type of the expression is adjusted toT
prior to any further analysis.The macro does thus never receive the information that the return type was
const
.
Possible workaround:#include <boost/type_traits/function_traits.hpp> // […] typedef boost::function_traits<BOOST_TYPEOF(f_const_int)>::result_type type;
Demo.
这篇关于BOOST_TYPEOF返回int const int的,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!