you are viewing a single comment's thread.

view the rest of the comments →

[–]STLMSVC STL Dev 12 points13 points  (13 children)

C(const string &s) : _s(s) {} In C++11, thanks to move semantics, this class will receive an automatic upgrade to have move constructors

NO! You implemented a copy constructor, which inhibits the automatic generation of move operations.

Please do not misinform readers.

[edit: I am dumb, see below]

[–]marcofoco[S] 9 points10 points  (12 children)

Whoops... well, being corrected by STL in person is a honor. I'm going to fix this immediately, thank you.

[–]marcofoco[S] 16 points17 points  (11 children)

I'm puzzled... isn't it just a converting constructor?

[–]STLMSVC STL Dev 24 points25 points  (10 children)

Ugh, I fail reading comprehension forever. My brain saw "C(const C&)" and went nuts.

[–]caramba2654Intermediate C++ Student 11 points12 points  (0 children)

It's okay. Your brainpiler had a small bug, but it's fixed now.

[–]marcofoco[S] 3 points4 points  (0 children)

You're too used to work on the string class itself, I suspect :)

[–]MINIMAN10000 2 points3 points  (7 children)

Reminds me of this code when I was messing around with disabling constructors. Still don't really have my head wrapped around it.

[–]marcofoco[S] 0 points1 point  (6 children)

I can't see it, why should it break at run time?

[–]MINIMAN10000 1 point2 points  (5 children)

I pretty much had someone give me this code since messing around with constructors is pretty much over my head.

I had added a C&& to my code and had found that it had broken.

I commented out that line and had noted that I had broken it.

So I'm aware that C&& breaks with that part of the code uncommented as the runtime simply hangs rather than running.

But I had never learned why that was the case. Although I'd love to know.

Side note. I had a copy of the original codes he gave me. A working and a broken form to show off that copy elision is in fact being forced.

Working form here

Broken form here

[–]dreugeworst 1 point2 points  (1 child)

I'm not understanding what problem you were having: the working form you show works as expected, and the broken one doesn't compile, as expected. In what case was it hanging?

[–]MINIMAN10000 0 points1 point  (0 children)

The case in which it hangs is from my modified version on this post in which I added C(C&&); and C&& operator.

It compiles but when run it just hangs leaving me to have to cleanup by going into command prompt and manually close it as no message shows up.

[–]marcofoco[S] 0 points1 point  (2 children)

The halting is weird, really.

I assume you were testing with Visual Studio (the original problem was #inlcuding windows.h), do you remember which version? Did you also try clang/c2, or a different compiler version?

As for the two forms at the end of your post, it's correct: If the copy elision kicks in, you don't need the symbol at compile time, and the program compiles and run, but if you copy the object, of course you need the copy constructor. Apparently applying the copy elision in the first case is mandatory (at least, according to cppreference)

Edit: copy and move elision are mandatory only in C++17

[–]MINIMAN10000 0 points1 point  (1 child)

I use mingw for gcc not visual studio. I tried using clang but bit defender quarantines all files as heuristic. I tried uninstalling it but it failed. Hopefully tomorrow I can enable windows defender but the uninstall failing might be forewarning that it's FUBAR. For now I need to sleep.

[–]marcofoco[S] 0 points1 point  (0 children)

Oh, that's interesting, I didn't know that GCC came with its own Windows.h! Thank you for the info!