C++ 结构化绑定和std::tuple的实现
确实,在C++ 结构化绑定和std::tuple的实现,c++,clang,c++17,stdtuple,structured-bindings,C++,Clang,C++17,Stdtuple,Structured Bindings,确实,在clang(我使用最近构建的clang版本4.0.0(trunk 282683))中,使用了中的一些东西来实现,比如括号init list可能使用中的东西 我编写了简单的代码,只是为了处理以下一些问题: 到目前为止还不错,但当我在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 = i
clang
(我使用最近构建的clang版本4.0.0(trunk 282683)
)中,使用了
中的一些东西来实现,比如括号init list可能使用
中的东西
我编写了简单的代码,只是为了处理以下一些问题:
到目前为止还不错,但当我在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;
^
包含在/home/user/test/main.cpp:1中的文件中:
在/home/user/test//test.hpp中包含的文件中:4:
在/usr/local/bin/./include/c++/v1/utility:193中包含的文件中:
/usr/local/bin/。/include/c++/v1/\u元组:29:14:致命错误:未定义模板“std::\uu 1::tuple\u size”的隐式实例化
:公共元组大小{};
^
/home/user/test/main.cpp:110:16:注意:在模板类的实例化中,此处请求了“std::\uu 1::tuple\u size”
常数auto[a,b,c]=S{1,'2',3.0};
^
/usr/local/bin/./include/c++/v1/u元组:25:50:注意:这里声明了模板
模板类_LIBCPP_TYPE_VIS_ONLY tuple_size;
^
即,与意外包含的
存在交互
我知道结构化绑定部分是在clang
中实现的,但不管怎样,
与它们之间的关系都很有趣
我应该包括
来使用结构化绑定吗
其他:
auto
,auto&
和auto&
工作,但auto-const
和auto-const&
不工作。是的,结构化绑定使用tuple\u size
和tuple\u元素
作为自定义点。基本规则大致是
tuple_size::value
李>
要使步骤2可靠地工作,
tuple\u size
需要对SFINAE友好,但目前不要求tuple\u size
对SFINAE友好。因此。使用std::tie实现对我来说是很自然的…@W.F.另一种方法是内置功能。我的意思是底层实现:)但我知道它不能回答你的问题…请看。您可以期望在C++17发布之前修复此问题。@W.F.tie
对我来说是完全不自然的,但不管怎样。有趣的是,它看起来像是一个基于SFINAE算法的等效(伪)代码(类似于远程for循环存在的代码)。为什么不包括
,这里就没有硬错误?真奇怪。因为如果没有符号std::tuple\u size
,那么这里肯定会有硬错误,但事实并非如此。而违反tuple\u size
的SFINAE友好性则会产生硬错误。@Orient它是一种特殊的核心语言结构;它不必遵守(正常的)SFINAE规则。可以说,在缺少tuple\u size
时不触发硬错误是有意义的,因为独立实现根本不需要tuple\u size
。
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;
^