结构化绑定实现地下和 std::tuple

Ori*_*ent 4 c++ clang stdtuple c++17 structured-bindings

这是真的,即结构化绑定clang(我用最近建造clang version 4.0.0 (trunk 282683))用一些东西来实现<tuple>,如括号,初始化列表可以使用的东西从<initializer_list>

我编写了简单的代码只是为了使用一些最新的功能

struct S { int a; char b; double c; };
auto [a, b, c] = S{1, '2', 3.0};
using A = decltype(a);
using A = int;
using B = decltype(b);
using B = char;
using C = decltype(c);
using C = double;
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利,但是当我const之前添加限定符时auto

struct S { int a; char b; double c; };
const auto [a, b, c] = S{1, '2', 3.0};
using A = decltype(a);
using A = int const;
using B = decltype(b);
using B = char const;
using C = decltype(c);
using C = double const;
Run Code Online (Sandbox Code Playgroud)

我收到一个奇怪的错误描述:

In file included from /home/user/test/main.cpp:1:
In file included from /home/user/test/./test.hpp:4:
In file included from /usr/local/bin/../include/c++/v1/utility:193:
/usr/local/bin/../include/c++/v1/__tuple:29:14: fatal error: implicit instantiation of undefined template 'std::__1::tuple_size<S>'
    : public tuple_size<_Tp> {};
             ^
/home/user/test/main.cpp:110:16: note: in instantiation of template class 'std::__1::tuple_size<const S>' requested here
    const auto [a, b, c] = S{1, '2', 3.0};
               ^
/usr/local/bin/../include/c++/v1/__tuple:25:50: note: template is declared here
template <class _Tp> class _LIBCPP_TYPE_VIS_ONLY tuple_size;
                                                 ^
Run Code Online (Sandbox Code Playgroud)

即与意外包含交互<tuple>

我知道,结构绑定在部分地实现clang,但无论哪种方式,有趣的是,如何<tuple>可能与他们?

我应该包括<tuple>使用结构化绑定吗?

额外的:

auto,auto &并且auto &&有效,但auto constauto const &没有。

T.C*_*.C. 6

是的,结构化的绑定使用tuple_size,并tuple_element作为定制点。基本规则大致是,

  1. 首先处理内置数组;
  2. 然后检查tuple_size<T>::value;
  3. 如果失败,则检查该类是否具有所有公共数据成员。

为了让第 2 步可靠地工作,tuple_size需要对 SFINAE 友好,但tuple_size<cv T>目前不需要对 SFINAE 友好。因此出现了错误