4

Is it true, that structured bindings in clang (I use recently builded clang version 4.0.0 (trunk 282683)) are implemented using some stuff from <tuple>, like braces-init lists may use stuff from <initializer_list>?

I wrote simple code just to play with some of latest features implemented:

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;

So far so good, but when I add const qualifier before 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;

I get a strange error description:

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;
                                                 ^

I.e. there is interaction with accidentally included <tuple>.

I know that structured bindings are partially implemented in clang, but either way it is interesting how <tuple> may be related to them?

Should I include <tuple> to use structured bindings?

Additional:

auto, auto & and auto && works, but auto const and auto const & not.

4

1 回答 1

6

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

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

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

于 2016-09-29T08:15:57.650 回答