基本的 C++ 03 枚举类型只是一个具有花哨名称的整数值,因此我希望按值传递它......
boost::call_traits<T>::param_type
出于这个原因,我还希望T=SomeEnum
确定最有效的传递方式T
是按值。
从 boost 文档中,请参阅Call Traits:
定义一个类型,表示将 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;
}