This change looks technically both binary and source compatible but it's not behavior compatible and leads to unexpected behavior at runtime.
Could it be possible to detect it and raise a warning or something like a behavior-incompatible error?
// file header1.hpp
class MyClass {
public:
int myAttr1;
int myAttr2;
int getMyAttr1() const {return myAttr1;}
int getMyAttr2() const {return myAttr2;}
};
// file main1.cpp
#include "header1.hpp"
#include <iostream>
int main() {
MyClass c = {42, 541};
std::cout << "myAttr1=" << c.getMyAttr1() << std::endl;
std::cout << "myAttr2=" << c.getMyAttr2() << std::endl;
}
/*
~/TestInitializerToConstructor
$ g++ --version && g++ -std=c++11 ./main1.cpp -o main1.exe && ./main1.exe
g++ (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
myAttr1=42
myAttr2=541
*/
Compare if we replace header1.hpp by header2.hpp (main is unchanged apart from the include).
// file header2.hpp
class MyClass {
public:
MyClass(int attr2, int attr1) : myAttr1(attr1), myAttr2(attr2) {}
int myAttr1;
int myAttr2;
int getMyAttr1() const {return myAttr1;}
int getMyAttr2() const {return myAttr2;}
};
// file main2.cpp
#include "header2.hpp"
#include <iostream>
int main() {
MyClass c = {42, 541};
std::cout << "myAttr1=" << c.getMyAttr1() << std::endl;
std::cout << "myAttr2=" << c.getMyAttr2() << std::endl;
}
/*
~/TestInitializerToConstructor
$ g++ --version && g++ -std=c++11 ./main2.cpp -o main2.exe && ./main2.exe
g++ (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
myAttr1=541
myAttr2=42
*/
Hello,
This change looks technically both binary and source compatible but it's not behavior compatible and leads to unexpected behavior at runtime. Could it be possible to detect it and raise a warning or something like a behavior-incompatible error?
Compare if we replace header1.hpp by header2.hpp (main is unchanged apart from the include).