JeJ*_*eJo 4 c++ iterator c++17 structured-bindings std-tie
我很想 通过以下方式解决这个问题:
#include <iostream>
#include <set>
#include <iterator>
#include <array>
#include <tuple>
#include <type_traits>
int main()
{
const std::set<int> s{ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
auto iter = s.find(5);
using IterType = decltype(iter);
// using `std::array` works fine!
const auto& [pv1, nxt1] = std::array<IterType, 2>{std::prev(iter), std::next(iter)};
std::cout <<"using std::array<IterType, 2> :"<< *pv1 << " " << *nxt1 << '\n'; // prints: 4 6
// using ` std::make_tuple` works fine!
const auto& [pv2, nxt2] = std::make_tuple(std::prev(iter), std::next(iter));
std::cout << "using std::make_tuple :" << *pv2 << " " << *pv2 << '\n'; // prints: 4 6
// using `std::tie` deduction happens in MSVC, but not in GCC and Clang
const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
// following is an assertion failure in MSVC with /O2 /std:c++17
std::cout << "using std::tie :" << *pv3 << " " << *nxt3<< '\n';
}
Run Code Online (Sandbox Code Playgroud)
我std::tieD的返回类型为迭代std::prev和std::next,并允许结构化结合做auto扣。
const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
Run Code Online (Sandbox Code Playgroud)
看起来它允许仅有的编译器是MSVC v19.14用/O2 /std:c++17!在GCC 9.1和铛8.0不同意这种说法。见在线编译器:https : //godbolt.org/z/DTb_OZ
GCC 说:
<source>:23:28: error: no matching function for call to 'tie'
const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
^~~~~~~~
/opt/compiler-explorer/gcc-8.3.0/lib/gcc/x86_64-linux-gnu/8.3.0/../../../../include/c++/8.3.0/tuple:1605:5: note: candidate function [with _Elements = <std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>>] not viable: expects an l-value for 1st argument
tie(_Elements&... __args) noexcept
^
Run Code Online (Sandbox Code Playgroud)
Clang 说:
<source>: In function 'int main()':
<source>:23:46: error: cannot bind non-const lvalue reference of type 'std::_Rb_tree_const_iterator<int>&' to an rvalue of type 'std::_Rb_tree_const_iterator<int>'
23 | const auto& [pv3, nxt3] = std::tie(std::prev(iter), std::next(iter));
| ~~~~~~~~~^~~~~~
In file included from <source>:5:
/opt/compiler-explorer/gcc-9.1.0/include/c++/9.1.0/tuple:1611:19: note: initializing argument 1 of 'constexpr std::tuple<_Elements& ...> std::tie(_Elements& ...) [with _Elements = {std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>}]'
1611 | tie(_Elements&... __args) noexcept
| ~~~~~~~~~~^~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
查看cppreference.com 中给出的示例, MSVC 是否正确?或谁(是)在这里,为什么?
有趣的是,在运行
std::cout << "using std::tie :" << *pv3 << " " << *nxt3<< '\n';
Run Code Online (Sandbox Code Playgroud)
给我
(在MSVS 2019 中,/std:c++17)
Run Code Online (Sandbox Code Playgroud)template<class... TTypes> constexpr tuple<TTypes&...> tie(TTypes&... t) noexcept;
这里,参数是非常量左值引用。 std::prev(iter)并且std::next(iter)不能绑定到左值引用,所以应该拒绝代码。MSVC 接受这一点的原因在评论中解释:
/Za对编译器启用标志,然后代码将被拒绝。MVSC [原文如此] 具有扩展功能,允许将临时值绑定到左值引用。tie得到左值的引用,但是prev,next返回暂时的。– rafix07 2019-07-25 07:52:58Z
| 归档时间: |
|
| 查看次数: |
323 次 |
| 最近记录: |