结构化绑定的函数声明

时间:2017-06-26 12:30:56

标签: c++ c++17 structured-bindings

结构化绑定只能与某种“struct”一起用作返回值吗?

回馈任何类/结构,例如这里的元组工作正常:

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

是否有能够实现以下内容的东西:

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

1 个答案:

答案 0 :(得分:5)

你不能拥有像

这样的结构
auto f() -> [int,double]

因为那里没有类型信息。尾随返回需要 type-id ,它被定义为 type-specifier-seq abstract-declarator opt

由于您必须在返回类型中指定类型,因此可以使用类似

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

指定您要返回intdouble

另请注意,structured bindings可用于具有公共数据成员,类似对象和数组的类。

相关问题