BOOST_TYPEOF返回int const int的,而不是 [英] BOOST_TYPEOF returns int instead of const int

查看:127
本文介绍了BOOST_TYPEOF返回int 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 not BOOST_TYPEOF return const int. How one can check if function returns const 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," where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆