为什么 boost::call_traits<T>::p aram_type 是枚举类型的引用?

Why is boost::call_traits<T>::param_type a reference for enumerated types?

本文关键字:枚举 type 引用 aram 类型 call boost traits lt gt 为什么      更新时间:2023-10-16

一个基本的 C++ 03 枚举类型只是一个带有花哨名称的整数值,因此我希望通过值传递它......

出于这个原因,我也希望boost::call_traits<T>::param_type T=SomeEnum确定最有效的传递T方式是按值。

从提升文档中,请参阅调用特征:

定义一个类型,该类型表示将 T 类型的参数传递给函数的"最佳"方法。

当我将boost::call_traits<T>::param_typeT=SomeEnum一起使用时,它决定了应该通过引用传递 SomeEnum。

我还希望C++11 class enums也按值传递。

测试代码:

#include <string>
#include <typeinfo>
#include <boost/call_traits.hpp>
#include <boost/type_traits/is_reference.hpp>
enum SomeEnum
{
    EN1_ZERO = 0,
    EN1_ONE,
    EN1_TWO,
    EN1_THREE
};
struct SomeStruct
{};
template<typename T>
void DisplayCallTraits( const std::string& desc )
{
    typedef typename boost::call_traits<T>::param_type param_type;
    std::cout << "-----------------------------------------------------n";
    std::cout << "Call traits for: " << desc << "n";
    std::cout << "ttypeof T : " << typeid(T).name() << "n";
    std::cout << "ttypeof param_type : " << typeid(param_type).name() << "n";
    std::cout << "tis_reference<param_type> : " << std::boolalpha 
              << boost::is_reference<param_type>::value << "n";
}
int main( int, char** )
{
    DisplayCallTraits< unsigned >( "unsigned" );     // pass by value, as expected
    DisplayCallTraits< SomeStruct >( "struct" );     // pass by reference, as expected
    DisplayCallTraits< SomeEnum >( "enumeration" );  // pass by reference - why?
    return 0;
}

这已更新为包括在实现中is_enum的检查。请参阅此错误报告,2 个月前关闭:https://svn.boost.org/trac/boost/ticket/5790