将这些模板转换为别名声明
Transforming these templates to Alias Declarations
我有一些基于编译时常量的模板,如下所示:
const int SIG0 = 0;
template<int Sig>
struct SignalPrototype;
template<>
struct SignalPrototype<SIG0> {
typedef std::function< void() > type;
};
当我试图将其转换为 C++11(我相信)别名声明时,我无法让它以任何形式或形式工作(只发布其中之一):
const int SIG0 = 0;
template<int Sig>
using SignalPrototype = std::function< void() >;
template<>
using SignalPrototype<SIG0> = std::function< void() >;
出现错误:expected unqualified-id before ‘using’
我想它期望模板参数中有一些东西,但我不能放SIG0
因为它不是一种类型。
笔记:我正在使用C++标准,最高可达 C++17,所以任何我不知道的新东西也值得赞赏。
另外,我不喜欢标题中的"这些",但我不知道它们的具体名称是什么。
这里有几件事是错误的。 const int SIG0 = 0;
需要成为constexpr
而不是const
。而且您不能专门化别名模板。
您可以做的是将这两种方法结合起来,如下所示:
constexpr int SIG0 = 0;
template <int Sig> struct SignalPrototype;
template<> struct SignalPrototype<SIG0> {
typedef std::function< void() > type;
};
template <int Sig>
using SignalPrototype_t = typename SignalPrototype<Sig>::type;
相关文章:
- C++使用默认模板参数键入别名和转发声明
- 为什么别名声明不是有效的 init 语句(/simple-declaration)?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- C++中的别名声明
- 应用于类型别名声明的 [[maybe_unused]] 属性的语法
- 是否可以将引用类型别名与指针运算符一起使用来声明对指针的引用?
- 替代班级内声明类型的别名
- 为什么我可以使用类型别名声明常量引用
- 如何使用别名声明模板化函数类型定义
- 将这些模板转换为别名声明
- 如何将所有别名、声明和初始化放在一起
- C++在不同的类中使用别名模板声明
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 无法声明函数中的模板类型别名
- 如果模板别名声明可以解决新的类型家庭
- 使用别名声明时出错
- 模板化别名声明的部分专门化
- c++概念生命和类型别名声明
- 在不同的命名空间中调用别名声明的基类构造函数
- 标准中哪里规定成员别名声明可以像静态成员一样使用