2

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
}
Klaus
  • 24,205
  • 7
  • 58
  • 113

1 Answers1

5

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.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402