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

5

我希望即使 T 是指针,const_value_type 仍然保持为 const。但是使用 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.
}

但我遇到了编译器错误: 缺少类型说明符-假定为int。
1个回答

5

clang错误信息会有所帮助。

typedef typename add_const_pointer<
//      ~~~~~~~~ Add typename
                  std::remove_pointer<T>, 
                  std::is_pointer<T>::value>::type 
                  const_value_type;

非常感谢...现在我不确定为什么我必须添加typename。 - Quest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接