BOOST_TYPEOF返回int而不是const int

时间:2015-01-30 17:36:19

标签: c++ boost const typeof

你能解释为什么打印1吗?不应BOOST_TYPEOF返回const int。如何在不使用c ++ 11功能的情况下检查函数是否返回const

#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;
}

1 个答案:

答案 0 :(得分:4)

如果prvalue表达式的类型为 cv int,则忽略该cv-qualifier。 [EXPR] / 6:

  

如果prvalue最初的类型为“ cv T,”其中T是   cv-unqualified non-class,non-array type,表达式的类型   在进行任何进一步分析之前调整为T

宏因此从未收到返回类型为const的信息 可能的解决方法:

#include <boost/type_traits/function_traits.hpp>

// […]
typedef boost::function_traits<BOOST_TYPEOF(f_const_int)>::result_type type;

Demo

相关问题