如何在模板中添加const限定符

Que*_*est 5 c++ templates

我想const_value_type成为const即使T是一个指针,这是不可能的std::add_const<>
所以我试过这样的事情:

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.
}
Run Code Online (Sandbox Code Playgroud)

但我得到编译器错误:缺少类型说明符 - 假定为int.

P0W*_*P0W 5

clang 错误消息会有所帮助

typedef typename add_const_pointer<
//      ~~~~~~~~ Add typename
                  std::remove_pointer<T>, 
                  std::is_pointer<T>::value>::type 
                  const_value_type;
Run Code Online (Sandbox Code Playgroud)

  • @Quest [This](http://stackoverflow.com/q/610245/1870232)会有所帮助. (2认同)