0

C++11 brought new keywords and new changes to core aspects of the language.
So could it be possible to successfully compile the same piece of code in both C++11 and pre-C++11, but get different results from each binary? By pre-C++11, I'm referring to C++98, C++03, or C++TR1.

The reason I ask, is because I have a bunch of small programs all written in C++. I am unaware what standard was in mind for each individual program written. Is the behaviour of these programs guaranteed to be the same if they all compile in C++11 as well as an earlier standard? I would like to compile them all in C++11(if they can be), but avoid any subtle changes that may cause the programs to behave differently had an earlier standard been in mind.

Working examples would be greatly appreciated.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • 1
    http://stackoverflow.com/questions/6399615/what-breaking-changes-are-introduced-in-c11 – chris Feb 16 '14 at 03:24
  • Is that the reason one writes unit tests and perform regression testing? – Ed Heal Feb 16 '14 at 03:24
  • @chris no, not breaking changes. changes across standards that still compile but behave differently – Trevor Hickey Feb 16 '14 at 03:25
  • @EdHeal agreed, but I'm interested in specifically knowing the non-breaking but differently behaving changes across C++ standards. – Trevor Hickey Feb 16 '14 at 03:28
  • 2
    @TrevorHickey, There are plenty of examples there that compile, but behave differently. – chris Feb 16 '14 at 03:29
  • @TrevorHickey: If the meaning of my code is changed, I still would consider it a breaking change. A change from "meaning X" to "meaningless" is just a particular instance of the more general notion of a change from "meaning X" to "meaning Y". – isekaijin Feb 16 '14 at 03:30
  • @TrevorHickey - Does testing identify when something behaves differently? How about optimization between different versions of the compiler? – Ed Heal Feb 16 '14 at 03:30
  • The item about narrowing conversions, if correct (I don't have the standard on this machine), is pretty bad. I recently answered a question where I was "pretty sure" that it couldn't break C++03 code wholesale this away. But apparently it does... – Cheers and hth. - Alf Feb 16 '14 at 03:57

1 Answers1

7

As chris points out, this is a duplicate of this question. However I did not see in the answers to that question the following:

#include <vector>
#include <iostream>

struct X
{
    X() {std::cout << "X()\n";}
    X(const X&) {std::cout << "X(const X&)\n";}
};

int
main()
{
    std::vector<X> v(3);
}

In C++03 this outputs:

X()
X(const X&)
X(const X&)
X(const X&)

In C++11 this outputs:

X()
X()
X()

For almost all code, this makes no difference. However "almost" is not "always", so this is a breaking (behavioral difference) change. You can blame me personally for this change. Without it:

std::vector<std::unique_ptr<int>> v(3);

would not have compiled. And I considered this case sufficiently motivating for the breakage.

Community
  • 1
  • 1
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577