Detecting const-ness of nested type

Posted by Channel72 on Stack Overflow See other posts from Stack Overflow or by Channel72
Published on 2011-01-13T18:38:18Z Indexed on 2011/01/13 18:53 UTC
Read the original article Hit count: 245

Filed under:
|
|
|

Normally, if I need to detect whether a type is const I just use boost::is_const. However, I ran into trouble when trying to detect the const-ness of a nested type. Consider the following traits template, which is specialized for const types:

template <class T>
struct traits
{
    typedef T& reference;
};

template <class T>
struct traits<const T>
{
    typedef T const& reference;
};

The problem is that boost::is_const doesn't seem to detect that traits<const T>::reference is a const type.

For example:

std::cout << std::boolalpha;
std::cout << boost::is_const<traits<int>::reference>::value << " ";
std::cout << boost::is_const<traits<const int>::reference>::value << std::endl;

This outputs: false false

Why doesn't it output false true?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates