为什么 boost::call_traits<T>::p aram_type 是枚举类型的引用?
Why is boost::call_traits<T>::param_type a reference for enumerated types?
一个基本的 C++ 03 枚举类型只是一个带有花哨名称的整数值,因此我希望通过值传递它......
出于这个原因,我也希望boost::call_traits<T>::param_type
T=SomeEnum
确定最有效的传递T
方式是按值。
从提升文档中,请参阅调用特征:
定义一个类型,该类型表示将 T 类型的参数传递给函数的"最佳"方法。
当我将boost::call_traits<T>::param_type
与T=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
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 访问在 C++ 结构中声明的枚举变量
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- typedef 枚举和枚举类有什么区别?
- 为什么我的开关/机箱在使用枚举时默认?
- 标准::可选枚举的比较运算符
- C++两个源文件之间共享的枚举的静态实例
- 打印没有铸件的枚举可以在C++中吗?
- 枚举成员与静态 int 成员?
- C++:枚举:错误:应使用标识符而不是"}"
- 带有 c++ 的枚举(输入检查)
- C++11 枚举前向导致"underlying type mismatch"
- 无法使用其他文件中的枚举:" does not name a type"错误
- 从整数到枚举的首选转换样式 QEvent::Type