2

Having the following example:

/* Signal Container */
template <typename Ret> class Signal;

template <typename Ret, typename... Args>
class Signal< Ret (Args...) >
{
    /* Following Implementation... */
};

/* Emitter Type */
template < template <typename Ret, typename... Args> Signal< Ret (Args...) > ... Sig>
class Emitter
{
    // using Signals = std::tuple<Sig...>;

    /* Following Implementation... */
};

/* Signals */
using Signal_A = Signal<void()>;
using Signal_B = Signal<void(int)>;
using Signal_C = Signal<void(int, float)>;

/* Desired Usage */
class MyType : public Emitter<Signal_A, Signal_B, Signal_C>
{

};

I would like to be able to inherit from an Emitter type that accepts 1 (0?) or more template parameters of the type Signal (and only Signal). Signal is a templated type and it's definition is different with each type passed to the Emitter arguments pack.

I tried the current approach on MinGW but I get these error messages and I'm a bit lost:

/* Line: template < template <typename Ret, typename... Args> Signal< Ret (Args...) > ... Sig> */
main.cpp|15|error: expected 'class' before 'Signal'|


/* Line: template < template <typename Ret, typename... Args> Signal< Ret (Args...) > ... Sig> */
main.cpp|15|error: expected '>' before '<' token|


/* Line: class MyType : public Emitter<Signal_A, Signal_B, Signal_C> */
main.cpp|29|error: wrong number of template arguments (3, should be 1)|


/* Linne: class Emitter */
main.cpp|16|error: provided for 'template<template<class Ret, class ... Args> class Signal> class Emitter'|

If anyone could clarify this or provide a working solution I'd be grateful.

Available compilers: MinGW GCC 4.9.2 (also 5.1.0)

SLC
  • 2,167
  • 2
  • 28
  • 46

3 Answers3

2

You can't do what you want in C++11 or C++14 in an easy way. Concepts might give us something, but for now your template arguments must be either a type, a class template, or a value. In your case, you want a pack of Signals which can only be specified as:

template <typename... Sigs>
class Emitter;

Within the class, you can then use static_assert to verify that they're all Signals:

static_assert(all_true<is_signal<Sigs>::value...>::value, "Emitter must use only Signals");

You'll have to write a type trait for is_signal, and provide a metafunction for all_true. One example of the latter can be found here

Community
  • 1
  • 1
Barry
  • 286,269
  • 29
  • 621
  • 977
  • Thank you. This is what I actually ended up with. All I wanted was to have a simple validation of the received types in the template pack. – SLC Jul 05 '15 at 03:34
1

You don't have to match all inner types of Signal in your class template Emitter. You just have to state it is a template receiving parameters. After that, you'll need to recur up to the minimum number of Signal<T>'s you may allow in your application. Supposing the minimum is one, here is a solution.

/* Signal Container */
template <typename Ret>
class Signal;

template <typename Ret, typename... Args>
class Signal<Ret(Args...)> {};

/* Emitter Type */
template <typename... Args>
class Emitter;

template <typename T>
class Emitter<Signal<T> > {
    // definition of class with single signal
};

template <typename T, typename... Args>
class Emitter<Signal<T>, Args...> {
    // definition of class with MORE THAN one signal
};

/* Signals */
using Signal_A = Signal<void()>;
using Signal_B = Signal<void(int)>;
using Signal_C = Signal<void(int, float)>;

/* Desired Usage */
class MyType : public Emitter<Signal_A, Signal_B, Signal_C> {};

int main() {
    MyType x;
}
Rubens
  • 14,478
  • 11
  • 63
  • 92
0

I'm posting this as an answer because it does fulfill the requirements in the question. Even though I marked a a correct answer. I think it's appropriate to post my final approach.

#include <iostream>

/* Signal Container */
template <typename Ret> class Signal;

template <typename Ret, typename... Args>
class Signal< Ret (Args...) >
{
    /* Following Implementation... */
};

namespace {
    /* Signal Type Traits */
    template < typename T >
    struct IsSignal { static constexpr bool Value = false; };

    template < typename T >
    struct IsSignal< Signal< T > > { static constexpr bool Value = true; };

    /* Signal Validation */
    template < bool V, typename... Args >
    struct AreSignals
    {
        static constexpr bool Value = V;
    };

    template < bool V, typename T, typename... Args >
    struct AreSignals< V, T, Args... >
    {
        static constexpr bool Value = AreSignals< V && IsSignal< T >::Value, Args... >::Value;
    };

}

/* Emitter Type */
template < typename... Args >
class Emitter
{
    // Block unsupported signals
    static_assert( AreSignals<true, Args...>::Value, "Unsupported signal type" );

    using Signals = std::tuple<Args...>;

    /* Following Implementation... */
};

using Signal_A = Signal<void()>;
using Signal_B = Signal<void(int)>;
using Signal_C = Signal<void(int, float)>;

class MyType : public Emitter<Signal_A, Signal_B, Signal_C>
{

};

int main(int argc, char **argv)
{
    std::cout << AreSignals<true, Signal_A, Signal_B, Signal_C>::Value << "\n"; // 1 (true)

    std::cout << AreSignals<true, Signal_A, int, Signal_B, Signal_C>::Value << "\n"; // 0 (false)

    return EXIT_SUCCESS;
}
SLC
  • 2,167
  • 2
  • 28
  • 46