-3

Can the copy constructor of std::string throw in C++11?

(Stackoverflow says my question does not meet its quality standards, and it seems like it just wants more prose, so here is some dummy text.)

Kristian Spangsege
  • 2,903
  • 1
  • 20
  • 43
  • 1
    I'm voting to close this question as off-topic because of the low quality question and the poster's poor quality attempt to lengthen it overcome the minimum question length limit. – marko Oct 15 '15 at 20:52
  • 2
    @marko, can you explain what the quality problem is? – Kristian Spangsege Oct 15 '15 at 20:57
  • We'd expect to see a bit more code than this. SO has a minimum question length limit try and persuade better quality questions. – marko Oct 15 '15 at 20:59
  • @marko, I see your point, but I like brevity, and I would hate to add text that serves no purpose. – Kristian Spangsege Oct 15 '15 at 21:02
  • 4
    @marko In general I would agree, but this question really doesn't require much code. It wouldn't add anything. – juanchopanza Oct 15 '15 at 21:02
  • 1
    It's low quality because _of course_ the copy constructor can throw. No research effort. – Jonathan Wakely Oct 16 '15 at 08:53
  • 1
    @JonathanWakely, it may be obvious to you, if you have studied `std::string` implementations, and what the standard requires about it, but otherwise it is far from obvious. For example, it seems to me that it would be possible to always copy a string object by copying the payload reference, and only clone the payload immediately before modification and before "leaks" that allow modification. – Kristian Spangsege Oct 18 '15 at 03:23
  • That has been addressed already on this site: http://stackoverflow.com/questions/12199710/legality-of-cow-stdstring-implementation-in-c11 so if the question is whether that's allowed, then it's a duplicate. – Jonathan Wakely Oct 18 '15 at 22:10
  • @JonathanWakely, thanks for digging up that link. It's certainly relevant. I don't see it as a duplicate, though. – Kristian Spangsege Oct 20 '15 at 07:27

1 Answers1

7

It can result in a memory allocation, and that can throw. So yes, it can throw.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480