c++11 - C++ disallow cast to rvalue reference -


as variables have been std::move'd, unsafe use them afterwards.

as have written code encourage user apply std::move on various occasions, want avoid used in wrong way, @ least in few crucial places (so selectively "protect against machiavelli").

therefore, following overload of std::move valid way go? or discourage using it?

struct {      void do_something() const { /* ... whatever ... */ } };  namespace std {     auto move(a& t) noexcept = delete;     auto move(a const& t) noexcept = delete;     //possibly same volatile qualifier      //possibly specialize static_cast<a const&>, static_cast<a&>, etc. }  // possibly set function "my_private_move" // can use exclusively appropriate.  int main() {     a;     // auto a_moved = std::move(a);      //disallow move of lvalue ref     a.do_something();                    //otherwise problematic      a(a{});                             //direct initialization via move constructor ok     auto x2 = a{};                       } 

your code exhibits undefined behavior, per [namespace.std]:

the behavior of c++ program undefined if adds declarations or definitions namespace std or namespace within namespace std unless otherwise specified. program may add template specialization standard library template namespace std if declaration depends on user-defined type , specialization meets standard library requirements original template , not explicitly prohibited.

your use case not fall under "otherwise specified" umbrella. besides being undefined, it's of questionable value... you're disallowing this:

a a;  f(std::move(a));  // don't use here 

despite being potentially performance improvement on f(a). user can still write cast explicitly accomplish same result:

f(static_cast<a&&>(a)); // more verbose move 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -