标签: std-tie

为什么结构化绑定会因 'std::tie' d 对象而失败?

我很想 通过以下方式解决这个问题:

#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] …
Run Code Online (Sandbox Code Playgroud)

c++ iterator c++17 structured-bindings std-tie

4
推荐指数
1
解决办法
323
查看次数

添加 : 在变量前面会给出错误的结果

我在比较班级。对于下面的代码

#include <string>
#include <set>
#include <tuple>
#include <cassert>


enum class e : bool
{
    positive = true,
    negetive = false
};


class A
{
public:
    int a;
    e e1 : 1;
    
  friend bool operator==(const A&, const A&);
};

 
 bool operator==(const A& lhs, const A& rhs) {
  auto tie = [](const A& a1) {
    return std::tie(a1.a, a1.e1);
  };
  auto x1 = tie(lhs);
  auto x2 = tie(rhs);
  return x1 == x2;   
}

int main()
{
A a1;
a1.a = 10;
a1.e1 …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 std-tie

4
推荐指数
1
解决办法
105
查看次数

标签 统计

c++ ×2

std-tie ×2

c++11 ×1

c++17 ×1

iterator ×1

structured-bindings ×1