c++11 - Why would const-ness of a local variable inhibit move semantics for the returned value? -


struct stest : public boost::noncopyable {     stest(stest && test) : m_n( std::move(test.m_n) ) {}     explicit stest(int n) : m_n(n) {}     int m_n; };  stest funcusingconst(int n) {     stest const a(n);     return a; }  stest funcwithoutconst(int n) {     stest a(n);     return a; }  void caller() {     // 1. compiles fine , uses move ctor     stest s1( funcwithoutconst(17) );      // 2. not compile (cannot use move ctor, tries use copy ctor)     stest s2( funcusingconst(17) ); } 

the above example illustrates how in c++11, implemented in microsoft visual c++ 2012, internal details of function can modify return type. until today, understanding declaration of return type programmer needs know understand how return value treated, e.g., when passed parameter subsequent function call. not so.

i making local variables const appropriate. helps me clean train of thought , structure algorithm. beware of returning variable declared const! though variable no longer accessed (a return statement executed, after all), , though variable declared const has long gone out of scope (evaluation of parameter expression complete), cannot moved , copied (or fail compile if copying not possible).

this question related question, move semantics & returning const values. difference in latter, function declared return const value. in example, funcusingconst declared return volatile temporary. yet, implementational details of function body affect type of return value, , determine whether or not returned value can used parameter other functions.

is behavior intended standard?
how can regarded useful?

bonus question: how can compiler know difference @ compile time, given call , implementation may in different translation units?


edit: attempt rephrase question.

how possible there more result of function declared return type? how seem acceptable @ function declaration not sufficient determine behavior of function's returned value? me seems case of fubar , i'm not sure whether blame standard or microsoft's implementation thereof.

as implementer of called function, cannot expected know callers, let alone monitor every little change in calling code. on other hand, implementer of calling function, cannot rely on called function not return variable happens declared const within scope of function implementation.

a function declaration contract. worth now? not talking semantically equivalent compiler optimization here, copy elision, nice have not change meaning of code. whether or not copy ctor called does change meaning of code (and can break code degree cannot compiled, illustrated above). appreciate awkwardness of discussing here, consider "bonus question" above.

a program ill-formed if copy/move constructor [...] object implicitly odr-used , special member function not accessible

-- n3485 c++ draft standard [class.copy]/30

i suspect problem msvc 2012, , not c++11.

this code, without calling it, not legal c++11:

struct stest {   stest(stest const&) = delete   stest(stest && test) : m_n( std::move(test.m_n) ) {}   explicit stest(int n) : m_n(n) {}   int m_n; };  stest funcusingconst(int n) {   stest const a(n);   return a; } 

because there no legal way turn a return value. while return can elided, eliding return value not remove requirement copy constructor exist.

if msvc2012 allowing funcusingconst compile, doing in violation of c++11 standard.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -