C++ 如何声明模板常量类型?

C++ 如何声明模板常量类型?,c++,templates,constants,C++,Templates,Constants,如果我做了一个typedef,比如 typedef int const cint; cint将引用无法修改的int。我可以在接受类型(模板参数、函数定义等)的任何上下文中使用cint 但是,typedef不能与模板一起使用。我希望能够声明一个像Constant这样的模板,并让它引用constsometype,就像我在上面使用cint所做的那样。有可能吗?C++11: template <typename T> using Constant = const T; Constant&

如果我做了一个typedef,比如

typedef int const cint;
cint
将引用无法修改的int。我可以在接受类型(模板参数、函数定义等)的任何上下文中使用
cint

但是,typedef不能与模板一起使用。我希望能够声明一个像
Constant
这样的模板,并让它引用
constsometype
,就像我在上面使用
cint
所做的那样。有可能吗?

C++11:

template <typename T>
using Constant = const T;

Constant<int> i = 1;
//! i = 2; // error: assignment of read-only variable 'i'
模板
使用常数=常数T;
常数i=1;
//! i=2;//错误:只读变量“i”的赋值
C++03:

template <typename T>
struct Constant
{
    typedef const T type;
};

Constant<int>::type i = 1;
模板
结构常数
{
typedef const T type;
};
常数::i型=1;

std::add_const_t
const SomeType
一样简单,只需使用常量=const t的
模板
使用常量=const T的模板@PiotrS。比我快7秒:(评论应该是答案,而且,我不记得了,但是“使用”模板使用Cx11功能吗?@NickoPo别名模板从C++11Wow开始就可用,比我预期的快多了。我正要将此添加到我的答案
+1