mar*_*ark 8 c++ templates boost
一个基本的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;
}
Run Code Online (Sandbox Code Playgroud)