add_const_ref

/* From C++ Template Metaprogramming Exercise 2.0:

Write a unary metafunction add_const_ref that returns T
if it is a reference type, and otherwise returns T const&.
Write a program to test your metafunction. Hint: you can
use boost::is_same to test the results.
*/

#include
#include
#include
#include
#include

using namespace std;
using namespace boost;

template
struct add_const_ref_helper;

template
struct add_const_ref
{
typedef typename
add_const_ref_helper::value>::type type;
};

template
struct add_const_ref_helper
{
typedef T type;
};

template
struct add_const_ref_helper
{
typedef T const& type;
};

int main()
{
cout << "Reference: " << is_same::type, int&>::value
<< endl; cout << "Non-Reference: " << is_same::type, const int&>::value
<< endl; return EXIT_SUCCESS; } [/code]

Similar Posts:

Comments 4