【发布时间】:2016-09-29 07:01:27
【问题描述】:
这是真的吗,clang 中的structured bindings(我使用最近构建的clang version 4.0.0 (trunk 282683))是使用来自<tuple> 的一些东西实现的,比如大括号初始化列表可能使用来自<initializer_list> 的东西?
我写了一些简单的代码只是为了玩一些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;
到目前为止一切都很好,但是当我在auto 之前添加const 限定符时:
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;
我收到一个奇怪的错误描述:
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;
^
即与意外包含的<tuple> 有交互。
我知道结构化绑定在 clang 中部分实现,但不管怎样,有趣的是 <tuple> 可能与它们有什么关系?
我应该包含<tuple> 以使用结构化绑定吗?
补充:
auto、auto & 和 auto && 有效,但 auto const 和 auto const & 无效。
【问题讨论】:
-
使用
std::tie实现对我来说很自然... -
@W.F.另一种方法是内置功能。
-
我的意思是底层实现 :) 但我知道它不能回答你的问题......
-
见LWG 2770。您可以期望在 C++17 发布之前修复此问题。
-
@W.F.
tie对我来说完全不自然,但无论如何。
标签: c++ clang c++17 stdtuple structured-bindings