typedef模板,这可能吗
typedef template, is this one possible?
是否可以执行以下操作:
template< typename T >
using T_CCUnit = static const constexpr T;
事实上,我只是想要一个static const constexpr
的别名,因为每次在某个文件中写都很无聊。我更喜欢c++11中的解决方案。
您可以很容易地为类型添加常量,因为cv限定符会修改类型:
template <typename T>
using T_C = std::add_const_t<T>;
但是,static
和constexpr
修改声明,而不是类型。所以,这里:
int x1;
const int x2;
static const int x3;
static constexpr const int x4;
// | ^ | type |name
// ^ | constexpr specifier
// storage class specifier
最后三个声明都属于const int
类型,但x2
与x3
和x4
具有不同的存储类。
老实说,如果主要问题是
写作真的很无聊。。。
我最好的建议是,你应该学习如何在你喜欢的编辑器中编写宏或保存常见的代码片段。
我认为对您来说最好的解决方案是使用宏。
#define STATIC_CONST_CONSTEXPR static const constexpr
但说真的,static
和constexpr
并不是该类型的修饰符。在您的情况下,使用using
所能做的就是const T
。其他的一切你要么自己写,要么请预处理器为你写。
static
存储限定符适用于实例,而不是类型。所以typedef
是不可能的。
您不能typedef static
或constexpr
,它们都不是类型的一部分。也就是说,即使你可以,也没有人会知道什么:
T_CCUnit<int> x = 4;
意思是,但每个人都会知道什么:
static constexpr int x = 4;
手段。每次额外键入7个字符以使代码更加易读是值得的。所有阅读您代码的人,以及您未来的自己,都会感谢您。
附带说明,const
与constexpr
是冗余的——声明的constexpr
的每个变量都隐式地为const
。
相关文章:
- 尝试根据类中 typedef 的存在来专门化模板函数
- 在类模板中使用 typedef 时出错
- 模板类中的 typedef 语句
- 模板类编译问题与 typedef 变量
- typedef名称与可变模板参数名称一致时出现GCC错误
- 如何对绑定的成员方法进行typedef,然后将该类型用作模板参数
- 模板中的Typedef导致阴影模板parm错误
- C++:显式调用模板参数的 typedef 的析构函数
- 通过 typedef 模板<类型名 T、T> 强制模板实例化 - 为什么有效?
- 在模板专用化中使用 typedef
- C Typedef修复模板ARGS
- 模板化类,包含由 typedef 定义的类对象的向量.如何称呼它们
- 如何将模板与Typedef一起使用
- 获得不取决于模板类的模板的Typedef
- 带有STD ::向量的模板Typedef具有自定义分配器
- C :在两个类中使用模板Typedef
- 如何访问可变模板参数包成员中的内部模板typedef
- 如何在模板类中使用c++模板typedef
- 对于类型修改特征,我是否应该为 typename::transform<...>::type 提供一个模板 typedef(又名使用)方便包装器?
- 使用boost图库的模板typedef汤