jeudi 25 février 2016

C++ 11 Comparing parameter packs

I am trying to wrap my head around parameter packs and need a little help.

Looking at the contrived example below, Is there a way to compare Args to T and only allow bar() to compile if they match? For example if I create Task<void(int, char, float)> I want bar(float, char, float) not to compile but bar(int, char, float) to compile just fine. Is this even feasible?

template <typename... Types>
struct foo {};

template<typename T>
struct Task;

template<typename R, typename...Args>
struct Task<R(Args...)>
{
    template<typename... T>
    std::enable_if<is_same<T, Args>
    void bar(T... args)
    {
        //do something here 
    }          
};

int main()
{
    Task<int(int)> task;
    int a = 0;
    float b = 1.0;
    bool c = false;

    //compiles
    task.bar(a);

    //none of these should compile
    task.bar(b);
    task.bar(c);
    task.bar(a, b);
    task.bar(a, b, c);
}

Aucun commentaire:

Enregistrer un commentaire