1

I've got a few templates based on compile time constants, like this:

const int SIG0 = 0;

template<int Sig>
struct SignalPrototype;

template<>
struct SignalPrototype<SIG0> {
  typedef std::function< void() > type;
};

When I tried to transform it into the C++11 (I believe) Alias Declarations, I couldn't get it to work in any shape or form (posting just one of them):

const int SIG0 = 0;

template<int Sig>
using SignalPrototype = std::function< void() >;

template<>
using SignalPrototype<SIG0> = std::function< void() >;

With the error: expected unqualified-id before ‘using’ I guess it is expecting something in the template parameters, but I can't put SIG0 as it is not a type.

Notes: I'm using C++ standard up to C++17, so anything newer that I don't know about is appreciated too.

Also, I don't like the 'these' in the title, but I have no idea what is their specific name.

niraami
  • 646
  • 8
  • 18

1 Answers1

3

There are several things that are wrong here. const int SIG0 = 0; needs to be a constexpr rather then const. And you cannot specialize alias templates.

What you could do is combine the two approaches like this:

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;
Community
  • 1
  • 1
SU3
  • 5,064
  • 3
  • 35
  • 66
  • I indeed could do that, I'll look if it fits my specific code - or of it even helps with readability (which is mainly what I'm after) – niraami Jan 11 '17 at 00:03
  • 1
    @areuz The `std` namespace in C++14 includes a lot of aliases like the `SignalPrototype_t` I suggested, which weren't there in C++11. So it seems that people find them useful. I use them myself too, because they spare you from typing `typename` and `::type` all the time. Here are some examples from the `type_traits` header: http://en.cppreference.com/w/cpp/header/type_traits Scroll down through the Synopsis section. – SU3 Jan 11 '17 at 00:08