Search code examples
c++c++14reinterpret-casttypename

using typename with reinterpret_cast()


How to create a typename that is the result of a reinterpret_cast?

For instance,

template<typename T1> class node{

public:

using null_sp2node = typename reinterpret_cast<shared_ptr<node<T1>>>(NULL);

};

The above generates the following compiler error:

error: expected a qualified name after 'typename'


Solution

  • reinterpret_cast returns a value, not a type. Specifically, it is an expression. If you want to get the type of an expression, the correct tool is decltype:

    using null_sp2node = decltype(reinterpret_cast<shared_ptr<node<T1>>>(NULL));
    

    However, because the result of a reinterpret_cast<T> is an expression of the type T, there's really no point to this compared to just:

    using null_sp2node = shared_ptr<node<T1>>;
    

    Of course, since reinterpret_cast<shared_ptr<node<T1>>>(NULL) is il-formed to begin with, it really doesn't matter. You cannot reinterpret_cast a null pointer constant to a non-pointer type (except for certain integer types).