#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
一切都会编译并运行。我还检查了如果我将变量 year
、month
和 day
声明为 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/