Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top