1
template<typename FirstArgT, typename...ArgsT>
class Server :public Server<ArgsT...> {
public:
    Server(const function<void (FirstArgT, ArgsT...)>& func) 
        :Server<ArgsT...>([&](ArgsT args...) -> void { func(arg0, args...); }) { }

private:
    FirstArgT arg0;
}

but the compiler says :

Error C3520 'ArgsT': parameter pack must be expanded in this context

Error C3546 '...': there are no parameter packs available to expand

in line 4 and 5.

Is it possible to use variadic parameters as parameters of a lambda is VS2015, or is there an alternative way to do it?

AlexWang
  • 27
  • 3
  • 2
    `ArgsT... args` not `ArgsT args...` – Piotr Skotnicki Sep 05 '15 at 09:39
  • [OT]: I can't tell what you are actually doing, but capturing `func` by reference may be a bad idea – Piotr Skotnicki Sep 05 '15 at 09:47
  • I want to write an rpc lib with c++11 features – AlexWang Sep 05 '15 at 09:56
  • the answer is so simple! but i have been struggling with it all afternoon.... – AlexWang Sep 05 '15 at 09:57
  • 1
    If you're about to store `func` eventually, you'll get UB if you keep capturing by reference objects you don't know the lifetime of – Piotr Skotnicki Sep 05 '15 at 09:59
  • yeah, thank you Piotr, i encountered that bug just now.. – AlexWang Sep 05 '15 at 10:06
  • Could you elaborate more about what you are trying to do? [OT] I estimate that you are trying to save the arguments for calling later through the template recursion. You could drop the template recursion and use std::tuple instead to store a variadic count of arguments and invoke your function later from the tuple. See http://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer for a working example. – Naios Sep 05 '15 at 10:34

1 Answers1

0

I extended and fixed your code to get it compiled. It would be nice if your next question comes with full example so that we have not the need to extend the rest of the example ;)

Indeed, I have no idea what you code is good for :-)

template<typename ...T> class Server;

template<typename FirstArgT, typename...ArgsT>
class Server<FirstArgT,ArgsT...> :public Server<ArgsT...> {
    public:
        Server(const std::function<void (FirstArgT, ArgsT...)>& func)
            :Server<ArgsT...>([&](ArgsT ... args)-> void { func(arg0, args...); }) { }

    private:
        FirstArgT arg0;
};

template<typename FirstArgT>
class Server<FirstArgT>
{
    public:
    Server(const std::function<void (FirstArgT)>& func) {}
};


void Do( int, double) {}


int main()
{
    Server<int,double> se( &Do );
}

If your intention is only to store the arguments somewhere and call the function with stored arguments, simply use std::bind.

void Do( int i, double d) { std::cout << i << " " << d << std::endl; }

int main()
{
    auto fx= std::bind( &Do, 1, 2.34);
    fx();

    // which works also for lambda:
    auto fx2=
     std::bind( []( int i,double d )->void
      { std::cout << i << " " << d << std::endl; }, 4, 5.6);

 }
Klaus
  • 24,205
  • 7
  • 58
  • 113