vendredi 30 septembre 2016

How to properly pass C strings to a lambda inside a variadic template function

Here is my complex situation:

I have a function using variadic template and lambda:

template<typename...Args>
void foo(Args...args) {
    // the arguments are passed to the lambda
    _func = [args...](){ do_sth(args...); };
}

On observing specific events, the lambda _func would be fired. My problem is, some of the arguments I passed are C strings, they could be pointing to temporary std string like this:

const char *pStr = __temp_std__string__.c_str();

Given I call foo(pStr);, and when _func is called, the temporary string that pStr is pointing to, has been released. I would like to know whether there exists a generic way to handle this. I am using C++11.

Aucun commentaire:

Enregistrer un commentaire