Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add const qualifiers in template

Tags:

c++

templates

I want to const_value_type be const even if T is a pointer, which is not possible with std::add_const<>
So I tried something like this:

template<typename value_type, bool is_pointer>
struct add_const_pointer{
    typedef const value_type type;
};

template<typename value_type>
struct add_const_pointer<value_type, true>{
    typedef const value_type *type;
};

template<typename T>
class Foo
{
public:
    typedef T value_type;
    typedef add_const_pointer<std::remove_pointer<T>, std::is_pointer<T>::value>::type const_value_type; 
    // here I get compiler error: missing type specifier - int assumed.
}

but I get compiler error: missing type specifier - int assumed.

like image 324
Quest Avatar asked Nov 20 '14 17:11

Quest


1 Answers

clang error message would help

typedef typename add_const_pointer<
//      ~~~~~~~~ Add typename
                  std::remove_pointer<T>, 
                  std::is_pointer<T>::value>::type 
                  const_value_type;
like image 121
P0W Avatar answered Oct 06 '22 00:10

P0W