简体   繁体   中英

How to use variadic parameter in lambda from variadic template class

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?

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);

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM