5

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?

Community
  • 1
  • 1
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
  • 1
    Do note that a class type with a conversion operator to e.g. function pointer type (a so-called surrogate call function) is a functor type and yet is not compatible with such a mechanism. It doesn't matter too much because those are exotic, but it's a bit frustrating not to be entirely generic (TTBOMK). – Luc Danton Oct 01 '12 at 01:25
  • 1
    [This](http://stackoverflow.com/questions/7870498/using-declaration-in-variadic-template) may be helpful (if not for an answer, at least for what is attempted). – Luc Danton Oct 01 '12 at 01:34
  • @Luc: Ain't that a direct duplicate? Voted to close as one. – Xeo Oct 01 '12 at 01:55
  • 1
    @HighCommander4: More generally, the parameter pack expansion is only allowed in a restricted number of situations. For example it is not supported for class/struct attributes either (fortunately, `std::tuple` is provided). – Matthieu M. Oct 01 '12 at 09:18

1 Answers1

-6

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • I meant to add a link to where I saw the mechanism in question, but I couldn't find the article or recall its name at the time I wrote the question. Thanks for digging it up! – HighCommander4 Oct 01 '12 at 01:53
  • The recursive solution is a nice idea. In fact, the article you linked to has a link in a footnote (https://gist.github.com/3779345) to such a solution. – HighCommander4 Oct 01 '12 at 01:59
  • @HighCommander4: heh, i didn't know. – Cheers and hth. - Alf Oct 01 '12 at 02:01
  • @anonymous downvoter: please explain your downvote, so that others can ignore it (and more easily ignore your future downvotes). tia. – Cheers and hth. - Alf Oct 01 '12 at 04:18
  • 8
    @Cheersandhth.-Alf: I think you were downvoted for your grumpiness. Personally I mostly vote based on content and readability, but some users may be sensible to the form as well. – Matthieu M. Oct 01 '12 at 09:17
  • 4
    I downvoted for grumpiness and because the content of the answer is pretty vague – radman Sep 24 '15 at 00:45