Home > Software engineering >  How can I expand this expression to the return types of callables?
How can I expand this expression to the return types of callables?

Time:01-15

Why can't I expand the parameter types?

auto lambda = []() { return 'c'; };

template <typename ... Ts>
struct MyClass
{

};

template <typename ... Ts>
void createMyClass(Ts&& ... ts)
{
    /* SUPPOSED TO CREATE MY CLASS WITH THE RETURN VALUES OF THE CALLABLES */
    MyClass< (decltype(std::declval<Ts>()()), ...)> d;

}


int main()
{
    createMyClass(lambda, lambda);

    
    return 0;
}

decltype(std::declval<Ts>()()) is supposed to get the type of the return value from a call to Ts. Then I try to expand that by doing , ... after it, and wrapping it in parentheses, which is a fold expression. The equivalent to what I'm looking for is essentially MyClass<return_type1, return_type2, return_type_n>.

"syntax error: ',' was unexpected here  

and

syntax error: unexpected token '...' following 'statement'  

CodePudding user response:

Should be

MyClass<decltype(std::declval<Ts>()())...>

or

MyClass<decltype(ts())...>

(Es, ...) would be a fold expression (with expression not type, so Es instead of Ts) using the comma operator.

CodePudding user response:

No parentheses are needed, no comma is needed.

MyClass< decltype(std::declval<Ts>()()) ... > d;
  • Related