将这些模板转换为Alias声明

时间:2017-01-10 23:48:23

标签: c++ linux c++11 templates c++17

我有一些基于编译时常量的模板,如下所示:

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,所以我不了解任何更新的东西。

另外,我不喜欢标题中的'这些',但我不知道它们的具体名称是什么。

1 个答案:

答案 0 :(得分:3)

这里有几件事是错的。 const int SIG0 = 0;需要constexpr,而不是const。 而你cannot specialize alias templates

你可以做的是结合这两种方法:

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;