rvalue-reference

overload resolution between lvalue reference and rvalue reference

只愿长相守 提交于 2021-02-18 09:56:26
问题 #include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 return 0; } Both functions are exact matches. Below is a quote from the standard: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... S1 and S2 are reference bindings (8.5.3) and neither refers to an

overload resolution between lvalue reference and rvalue reference

旧巷老猫 提交于 2021-02-18 09:55:50
问题 #include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() { int arr[6]; func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1 return 0; } Both functions are exact matches. Below is a quote from the standard: Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if ... S1 and S2 are reference bindings (8.5.3) and neither refers to an

When should I declare a move constructor without noexcept?

拟墨画扇 提交于 2021-02-08 13:37:11
问题 The standard doesn't enforce noexcept on move constructors. In what circumstances is it acceptable/neccesary for a move constructor to throw? 回答1: When you really have no choice. Most of the time your move constructor should be noexcept . And they are by default. See this: http://www.codingstandard.com/rule/12-5-4-declare-noexcept-the-move-constructor-and-move-assignment-operator/ It is especially important to use noexcept for types that are intended to be used with the standard library

Why can't std::as_const(T &&v) move-return its argument?

痴心易碎 提交于 2021-01-21 09:20:02
问题 Reading Why does as_const forbid rvalue arguments? I understand that we can't convert a rvalue-ref into an lvalue-ref, of course. But why not move the rvalue-ref into a value and return that, i.e. ? template<typename T> const T as_const(T&& val) { return std::move(val); } This ought to work nicely with COW containers as well, as the returned value is const and iterators from it will not cause it to detach. Maybe some godbolt-ing will answer this though, but I can't think of a given scenario

Why can't std::as_const(T &&v) move-return its argument?

倖福魔咒の 提交于 2021-01-21 09:18:21
问题 Reading Why does as_const forbid rvalue arguments? I understand that we can't convert a rvalue-ref into an lvalue-ref, of course. But why not move the rvalue-ref into a value and return that, i.e. ? template<typename T> const T as_const(T&& val) { return std::move(val); } This ought to work nicely with COW containers as well, as the returned value is const and iterators from it will not cause it to detach. Maybe some godbolt-ing will answer this though, but I can't think of a given scenario