Вопрос

Possible Duplicate:
using declaration in variadic template

I recently came across a generic mechanism for combining two function objects to form a new function object that behaves as if the first two were overloaded:

template <typename F1, typename F2>
struct overload : public F1, public F2
{
    overload(F1 f1, F2 f2) : F1(f1), F2(f2) {}

    using F1::operator();
    using F2::operator();
};

I am trying to extend this idea to work for N function objects, using variadic templates:

template <typename... Fs>
struct overload : public Fs...
{
    overload(Fs... fs) : Fs(fs)... {}

    using Fs::operator();...
};

However, GCC complains about my attempt to do variadic expansion on the using-declaration:

test.cpp:6:24: error: parameter packs not expanded with '...':
     using Fs::operator();...
                        ^
test.cpp:6:24: note:         'Fs'
test.cpp:6:26: error: expected unqualified-id before '...' token
     using Fs::operator();...
                          ^

I've tried some variations, such as:

using Fs::operator()...;

and

using Fs...::operator();

but neither do the trick.

Is it possible to do this?

Это было полезно?

Решение

Instead of “I recently came across a generic mechanism”, please provide references such as “I recently came across a blog posting by Dave Abrahams describing a generic mechanism”….

I doubt that parameter pack expansion is supported for using statement, and I’m sorry I don’t have the time to check the standard – that’s again your job, to read the documentation before asking the question.

But as a practical matter of solving the problem at hand (rather than the language question), you can always code this up recursively instead of using direct expansion. Just inherit from a class that brings in the rest of the functors, then bring in the first one. Where “the rest” is the recursive step, terminating at an empty list of functors as base case.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top