c++ - 为什么 std::tie 不能与 const 类方法一起使用?

标签 c++ class c++11 tuples member-functions

#include <string>
#include <tuple>
#include <stdexcept>
using namespace std;

class Date
{
    public:
        Date() {};
    Date(int new_year, int new_month, int new_day)
    {
        year = new_year;

        if ((new_month < 1) || (new_month > 12))
        {
            throw runtime_error("Month value is invalid: " + to_string(new_month));
        }
        month = new_month;

        if ((new_day < 1) || (new_day > 31))
        {
            throw runtime_error("Day value is invalid: " + to_string(new_day));
        }
        day = new_day;
    }

    int GetYear() const
    {
        return year;
    }

    int GetMonth() const
    {
        return month;
    }

    int GetDay() const
    {
        return day;
    }

    private:
        int year;
    int month;
    int day;
};

bool operator < (const Date &lhs, const Date &rhs)
{
    auto lhs = tie(lhs.GetYear(), lhs.GetMonth(), lhs.GetDay());
    auto rhs = tie(rhs.GetYear(), rhs.GetMonth(), rhs.GetDay());

    return lhs < rhs;
}

我正在尝试创建一个用于存储日期(年、月、日)的类。为了在 map 中使用此类,我想重载比较运算符。不幸的是,上面的代码不起作用。编译器给我一个错误

error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'|

错误显然发生在 tie 函数中。一旦我将它更改为 make_tuple 一切都会编译并运行。我还检查了如果我将变量 yearmonthday 声明为 public 我可以写类似的东西

auto lhs = tie(lhs.year, lhs.month, lhs.day);

这也行。

我不明白为什么。有没有人有想法?

最佳答案

您的成员函数返回成员的拷贝(又名临时右值)和 std::tie 有参数列表,它尝试通过非成本左值引用进行绑定(bind)。

template< class... Types >
constexpr std::tuple<Types&...> tie( Types&... args ) noexcept;
//                                  ^^^^^^^^^^^^^^^^

is simply not possible as per standard ,因此错误!


std::make_tuple 另一方面有转发引用。因此,没有问题。

template< class... Types >
std::tuple<VTypes...> make_tuple( Types&&... args );
//                               ^^^^^^^^^^^^^^^^

当成员公开时,他们就变成了左值。


我建议,制作 operator<而是类的 friend 。

class Date 
{
public:
  // .... other code
  friend bool operator<(const Date& lhs, const Date& rhs) noexcept
  {
    return std::tie(lhs.year, lhs.month, lhs.day) < std::tie(rhs.year, rhs.month, rhs.day);
  }

private:
  int year;
  int month;
  int day;
};

关于c++ - 为什么 std::tie 不能与 const 类方法一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69951144/

相关文章:

其他类的C++纯虚方法实现

c++ - 重新使用或清理多维 vector

c++ - 将 std::atomic_flag 与工作线程一起使用时出现问题

c++ - 错误 : expected unqualified-id before ‘const’ on line 8

c++ - 为什么 std::async 比简单的分离线程慢?

c++ - 基于 CMake 的 CUDA 应用程序构建失败 - 没有文件传递给链接器

c++ - 我可以使用自定义样板模板创建 Qt Creator 项目吗?

Python类返回值

javascript - 如何将一个类实例插入另一个类的私有(private)数组中?

java - 为什么枚举在这种情况下比 HashMap 更有用?