结构化绑定实现地下层和std::元组
Structured bindings implementation underground and std::tuple
是真的吗,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;
到目前为止还不错,但是当我在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 &
不行
是的,结构化绑定使用tuple_size
和tuple_element
作为自定义点。基本规则大致是
- 首先处理内置数组;
- then check
tuple_size<T>::value
; - 如果失败,则检查类是否具有所有公共数据成员。
为了使步骤#2可靠地工作,tuple_size
需要是sfinae友好的,但tuple_size<cv T>
目前不需要是sfinae友好的。
相关文章:
- 专用于 std 元组的模板,而无需用户执行remove_cvref
- std::元组分配和复制/移动异常保证
- std::元组和移动语义
- std::元组作为成员替换,方便宏
- 如何从参数包构造引用的 std::元组?
- 用一个额外的元素扩展 std::array 的每个 std::元组
- std::元组大小,是不是错过了优化?
- 初始化语句中临时 std::元组的开销
- 如何改善STD ::元组在空置领域
- 就地解压缩 std::元组,而无需为 std::index_sequence 提供人工层
- 动态创建和扩展STD ::元组为参数包
- 获取std ::元组元素作为std ::变体
- std::元组到元组的映射和使用 emplace
- 检查是否所有 std::元组元素都满足条件 + 设计问题
- Q:std::元组/std::数组
- 为什么GCC警告使用STD ::元组和虚拟继承来调用非平凡的动作分配运算符
- 为什么使用std ::元组和模板派生的类扣除/替换会扣除/替换
- C++ 如何根据部分特化制作 std::元组类型?
- 由std ::元组存储的垃圾价值
- 使用 C++11 std::元组在大型项目中