简体   繁体   English

std::tie 用于 C++17 结构运算符<!--?</div--><div id="text_translate"><p> 考虑以下 C++17 结构:</p><pre> struct S { M1 m1; M2 m2; M3 m3; bool operator<(const S& that) const { return tie() < that.tie(); } auto tie() const { return std::tie(m1,m2,m3); } };</pre><p> 这个对吗? S::tie会返回成员引用的元组,还是会复制一份? 会自动推断出正确的类型(引用元组)吗? constness 做正确的事吗?</p><p> (我看到的示例对 std::tie 进行了两次调用,并且没有像这样分解为单独的成员 function。想知道/怀疑是否有原因。)</p></div>

[英]std::tie for C++17 struct operator<?

Consider the following C++17 struct:考虑以下 C++17 结构:

struct S {
   M1 m1; M2 m2; M3 m3;

   bool operator<(const S& that) const { return tie() < that.tie(); }

   auto tie() const { return std::tie(m1,m2,m3); }
};

Is this correct?这个对吗? Will S::tie return a tuple of references to the members, or will it take a copy? S::tie会返回成员引用的元组,还是会复制一份? Will auto deduce the correct type (a tuple of references)?会自动推断出正确的类型(引用元组)吗? Does the constness do the right thing? constness 做正确的事吗?

(The examples that I've seen make two calls to std::tie and don't factor out into a seperate member function like this. Wondering / suspicious if there is a reason for that.) (我看到的示例对 std::tie 进行了两次调用,并且没有像这样分解为单独的成员 function。想知道/怀疑是否有原因。)

Is this correct?这个对吗?

Yes, although that may depend on what you are intending to do.是的,尽管这可能取决于您打算做什么。

Will S::tie return a tuple of references to the members S::tie 会返回成员引用的元组吗

Yes.是的。

or will it take a copy?还是需要复印件?

No. This is easy to verify by trying it with a non-copyable type.不,这很容易通过尝试使用不可复制的类型来验证。

Will auto deduce the correct type (a tuple of references)?会自动推断出正确的类型(引用元组)吗?

Yes.是的。


This won't be necessary nor useful anymore in C++20 with the introduction of defaulted comparison operators.随着默认比较运算符的引入,这在 C++20 中不再是必需的也不再有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM