rvalue

Why is overloading on just one ref-qualifier not allowed?

时光怂恿深爱的人放手 提交于 2021-02-07 11:30:43
问题 Apparently, overloading on ref-qualifiers is not allowed – this code won't compile if you remove either & or && (just the tokens, not their functions): #include <iostream> struct S { void f() & { std::cout << "Lvalue" << std::endl; } void f() && { std::cout << "Rvalue" << std::endl; } }; int main() { S s; s.f(); // prints "Lvalue" S().f(); // prints "Rvalue" } In other words, if you have two functions of the same name and type, you have to define both if you define either . I assume this is

Insert in unordered map calls constructor

无人久伴 提交于 2021-01-28 02:52:45
问题 In order to avoid duplication of elements, I'm building a class that holds elements and provide an acces to them. My elements ( DynLibrary ) are movable but not copyable class DynLibrary { public: DynLibrary() : _handle(nullptr) {} DynLibrary(const std::string& path) { DynLibrary::open(path); } DynLibrary(const DynLibrary&) = delete; DynLibrary(DynLibrary&&) = default; ~DynLibrary() { DynLibrary::close(); } ... } Those object are allocated in an unordered_map which key is the path that

Why does as_const forbid rvalue arguments?

你离开我真会死。 提交于 2021-01-21 12:15:08
问题 I wanted to ask why as_const forbids rvalue arguments, according to cppreference.com (i.e. why the Standards folks made it so, not why cppreference.com specifically quoted them on that. And also not where in the spec the intent of the committee is codified, just for making sure :))). This (artificial) example would yield an error (user wants to make it const to keep COW quiet) QChar c = as_const(getQString())[0]; Another question's answer notes that if we just remove the deletion of the

Why does as_const forbid rvalue arguments?

纵饮孤独 提交于 2021-01-21 12:13:30
问题 I wanted to ask why as_const forbids rvalue arguments, according to cppreference.com (i.e. why the Standards folks made it so, not why cppreference.com specifically quoted them on that. And also not where in the spec the intent of the committee is codified, just for making sure :))). This (artificial) example would yield an error (user wants to make it const to keep COW quiet) QChar c = as_const(getQString())[0]; Another question's answer notes that if we just remove the deletion of the

Pass lvalue to rvalue

谁说我不能喝 提交于 2021-01-21 07:16:48
问题 I made a small 'blocking queue' class. It irritates me that I have created redundant code for values passed into the enqueue member function. Here are the two functions that do the same exact thing (except the rvalue uses std::move to move the rvalue into the actual queue collection), except handles lvalue and rvalue respectively: void enqueue(const T& item) { std::unique_lock<std::mutex> lock(m); this->push(item); this->data_available = true; cv.notify_one(); } void enqueue(T&& item) { std:

Assign a value to an rvalue reference returned from function

牧云@^-^@ 提交于 2020-06-27 07:17:32
问题 #include <utility> template <typename Container> decltype(auto) index(Container &&arr, int n) { return std::forward<Container>(arr)[n]; } Make a function call : #include <vector> index(std::vector {1, 2, 3, 4, 5}, 2) = 0; When function calling finished, the object std::vector {1, 2, 3, 4, 5} will be destroyed, assigning a value to a deallocated address would cause undefined behaviour. But the above code works well and valgrind detected nothing. Maybe the compile helps me make another

function template does not recognize lvalue

我的梦境 提交于 2020-02-03 05:48:25
问题 I have a problem in my code Here is simplified version of it : #include <iostream> class A { public : template <class T> void func(T&&)//accept rvalue { std::cout<<"in rvalue\n"; } template <class T> void func(const T&)//accept lvalue { std::cout<<"in lvalue\n"; } }; int main() { A a; double n=3; a.func(n); a.func(5); } I expect the output to be : in lvalue in rvalue but it is in rvalue in rvalue why ?! 回答1: template <class T> void func(T&&) is universal reference forwarding reference . To

function template does not recognize lvalue

删除回忆录丶 提交于 2020-02-03 05:48:05
问题 I have a problem in my code Here is simplified version of it : #include <iostream> class A { public : template <class T> void func(T&&)//accept rvalue { std::cout<<"in rvalue\n"; } template <class T> void func(const T&)//accept lvalue { std::cout<<"in lvalue\n"; } }; int main() { A a; double n=3; a.func(n); a.func(5); } I expect the output to be : in lvalue in rvalue but it is in rvalue in rvalue why ?! 回答1: template <class T> void func(T&&) is universal reference forwarding reference . To

What is decltype(0 + 0)?

我与影子孤独终老i 提交于 2020-01-11 08:05:08
问题 (Prompted by an answer.) Given N3290, §7.1.6.2p4, where the list items are unnumbered, but numbered here for our convenience: The type denoted by decltype(e) is defined as follows: if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed; otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e