Prev: Call for Paper The International Journal of Computer Science (IJCS)
Next: vector:erase iterator requirements
From: Paul Bibbings on 6 Feb 2010 04:19 Olivier <olivier.grant(a)gmail.com> writes: > On Feb 5, 8:47 pm, Paul Bibbings <paul.bibbi...(a)gmail.com> wrote: >> Olivier <olivier.gr...(a)gmail.com> writes: >> > Daniel, your solution is definitely very elegant, and I looking >> > forward to C++0x for this. But there is still something I don't seem >> > to get with rvalues. Take the following code which is an extension of >> > your solution : >> >> > 3 >> > 4 class Bad >> > 5 { >> > 6 public: >> > 7 explicit Bad( int const &i ) : i_(i) { } >> > 8 explicit Bad( int const && ) = delete; >> > 9 >> > 10 private: >> > 11 int const &i_; >> > 12 }; >> > 13 >> > 14 class Bad2 >> > 15 { >> > 16 public: >> > 17 explicit Bad2( Bad const &t ) : t_(t) { } >> > 18 explicit Bad2( Bad const && ) = delete; >> > 19 >> > 20 private: >> > 21 test const &t_; >> > 22 }; >> > 23 >> > 24 int main( ) >> > 25 { >> > 26 int i(23); >> > 27 >> > 28 //Bad t0(23); // <- Kaboom! Expected. >> > 29 Bad t1(i); >> > 30 >> > 31 Bad2 t20(Bad(i)); // <- No Kaboom ? Why ? >> > 32 Bad2 t21(t1); >> > 33 >> > 34 return 0; >> > 35 } >> > 36 >> >> > I would have (naively) expected to be able to do the same thing with >> > non built-in types. But this compiles fine with GCC 4.4 (line 28 blows >> > up if uncommented). So what i'm not getting is why does 23 bind to an >> > rvalue reference, where a call to Bad(i) does not ? What am I >> > overseeing ? >> >> Try replacing your line 31 with: >> >> Bad2 t20 = Bad2(Bad(i)); >> >> If you then still need help working out why your original line 31 seemed >> to be accepted by the compiler when this one isn't, replace the original >> with: >> >> Bad2 t20(Bad i); >> >> which is, in truth, a mere notational alteration that doesn't affect its >> semantics at all in this instance. > > Ok, now I'm completely confused :) > > Bad2 t20(Bad i); > > This is totally ill-formed to me. I'm not even sure how the compiler > should/does interpret that. But : > > Bad2 t20(Bad(i)); > > This isn't. Why is the compiler choking on this? How is it > interpreting this statement? Is this GCC specific? > > Sorry, I know this is slightly off-topic, but still interesting I > thinnk. It is indeed interesting, and very important to be aware of. You can get the background by searching on the internet for C++'s "most vexing parse." A quick introduction by Danny Kalev on InformIT is: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=439 where he puts it in the context of what he (optimistically) calls C++09. Regards Paul Bibbings -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |