Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

A basic C++ 03 enumerated type is just an integral value with a fancy name, therefore I would expect to pass it by value....

For this reason I would also expect boost::call_traits<T>::param_type with T=SomeEnum to determine that the most efficient way of pass T is by value.

From boost documentation see Call Traits:

Defines a type that represents the "best" way to pass a parameter of type T to a function.

When I use boost::call_traits<T>::param_type with T=SomeEnum it determines that SomeEnum should be passed by reference.

I would also expect C++11 class enums to also be passed by value.

Test Code:

#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;
}
like image 978
mark Avatar asked Nov 16 '12 10:11

mark


1 Answers

This has been updated to include a check to is_enum in the implementation. See this bug report, closed 2 months ago: https://svn.boost.org/trac/boost/ticket/5790

like image 147
BoBTFish Avatar answered Nov 01 '22 07:11

BoBTFish