Refactoring CPP03 --> CPP0x

E

er

Hello, please help with the problem below. Questions are within the
code.

#include <utility>
#include <boost/mpl/assert.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <boost/type_traits/remove_const.hpp>

// Implem developed under CPP03 that I'd like to not have to re-write
under CPP0x
template<typename T> void impl(T&){}

// CPP03
template<typename T> void f(T& t){ impl( t ); }
template<typename T> void f(T const& t){ impl( t ); }

// CPP0x - tentative replacement for f
template<typename T> void g(T&& t)
{

impl(
std::forward<T>( t )
// what should I have here, instead, such that I can emulate the
behavior of f when t is a rvalue?
);
}

int const x = -1;

f( x );
f( 1 );

g( x );
g( 1 ); // fails.

// sheds light on the problem:

template<typename T>
void assert_lvalue(T&&)
{
typedef boost::is_reference<T> pred_;
BOOST_MPL_ASSERT((pred_));
}

template<typename T>
void assert_rvalue(T&&)
{
typedef boost::is_reference<T> pred_;
BOOST_MPL_ASSERT_NOT((pred_));
}

template<typename T>
void assert_const(T&&)
{
typedef typename boost::remove_reference<T>::type val_;
typedef boost::is_const<val_> pred_;
BOOST_MPL_ASSERT((pred_));
}

assert_lvalue( x );
assert_const( x );
assert_rvalue( 1 );
assert_const( 1 ); // mpl::failed
 
E

er

Oops, I realize I already asked this and the matter was settled. I'll
probably rephrase it later because I had something in mind as well.
 
E

er

Oops, I realize I already asked this and the matter was settled. I'll

like this :

template<typename T> void g(T&& t)
{
impl(
t // instead of std::forward<T>( t )
);
}

probably rephrase it later because I had something in mind as well.

Consider this modification to the body of impl:

template<typename T> void impl(T&){
typedef boost::is_const<T> pred_;
std::cout << "pred_::value = " << pred_::value << std::endl;
}

then,

int const x = -1;
int y = 1;

f( x );
f( y );
f( 1 );

g( x );
g( y );
g( 1 );

outputs :

pred_::value = 1
pred_::value = 0
pred_::value = 1
pred_::value = 1
pred_::value = 0
pred_::value = 0

Could someone help me make sense of the last line? And perhaps suggest
an alternative solution such that a T = const int is passed to impl(),
in this case?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,717
Messages
2,569,382
Members
44,704
Latest member
FawnBernay

Latest Threads

Top