Klaus
Klaus

Reputation: 25663

function declaration for structured bindings

Can structured bindings only be used with some kind of "struct" as return value?

Give back any of class/struct e.g. a tuple here works fine:

auto f() 
{   
    return std::make_tuple(1,2.2);
}

Is there something which enables something like:

auto f() -> [int,double]
{
    return { 1, 2.2 }; //  maybe with or without braces arround
}

Upvotes: 2

Views: 920

Answers (1)

NathanOliver
NathanOliver

Reputation: 181027

You cannot have a construct like

auto f() -> [int,double]

as there is no type information there. The trailing return expects a type-id which is defined as a type-specifier-seq abstract-declaratoropt

Since you have to specify a type in the return type you can use something like

auto f() -> std::tuple<int,double>

to specify you want to return a int and double.

Also note that structured bindings can be used on classes with public data members, tuple like objects, and arrays.

Upvotes: 5

Related Questions