add_const_ref type transformation metafct

Hello, I wish to have a metafct that adds "const &" to types that have it or don't have it... template< typename T > struct add_const_ref { typedef typename boost::remove_const<T>::type t1; typedef typename boost::add_const<t1>::type t2; typedef typename boost::add_reference<t2>::type type; }; Does this exist already in type traits or elsewhere? I couldn't find.... Can this be improved in terms of reducing the number of tmpl instantiations? Note : add_const_ref< const double& >::type is const double& for e.g. Rds,

I wish to have a metafct that adds "const &" to types that have it or don't have it...
template< typename T > struct add_const_ref { typedef typename boost::remove_const<T>::type t1; typedef typename boost::add_const<t1>::type t2; typedef typename boost::add_reference<t2>::type type; };
Does this exist already in type traits or elsewhere? I couldn't find.... Can this be improved in terms of reducing the number of tmpl instantiations?
You don't need the remove_const<> in there - add_const has no effect if the type is already const. The open question is what do you want to have happen if the argument is a reference to a non-const type? If you want the result to be a const reference then: template< typename T > struct add_const_ref { typedef typename boost::remove_reference<T>::type t1; typedef typename boost::add_const<t1>::type t2; typedef typename boost::add_reference<t2>::type type; }; HTH, John.

Hicham Mouline wrote:
I wish to have a metafct that adds "const &" to types that have it or don't have it...
template< typename T > struct add_const_ref { typedef typename boost::remove_const<T>::type t1; typedef typename boost::add_const<t1>::type t2; typedef typename boost::add_reference<t2>::type type; };
Does this exist already in type traits or elsewhere? I couldn't find....
How about boost::call_traits<T>::param_type? http://www.boost.org/doc/libs/1_38_0/libs/utility/call_traits.htm
participants (3)
-
Hicham Mouline
-
John Maddock
-
Nat Goodspeed