I have constructed the following minimal example of my problem:
#include <iostream>
struct Foo {
Foo() {
std::cout << "default" << std::endl;
}
Foo(Foo& f2) {
std::cout << "non-const" << std::endl;
}
Foo(const Foo& f2) {
std::cout << "const" << std::endl;
}
};
int main() {
std::pair<Foo, int> foop0(Foo(), 1);
std::cout << std::endl;
std::pair<const Foo, int>foop1(foop0);
}
On my Ubuntu machine g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 will print out the following:
$ g++ -std=c++14 test.cpp -o test && ./test
default
const
const
However, Apple clang (version 11.0.3 (clang-1103.0.32.62) Target: x86_64-apple-darwin19.4.0) on my Mac will print:
$ g++ -std=c++14 test.cpp -o test && ./test
default
const
non-const
However, it gets worse: If i change the last line to
std::pair<Foo, int>foop1(foop0);
^ removed const
both compilers will give the first output.
Why does this happen?
EDIT: I have now understood why, according to cppreference, std::pair's ctors should be selected as they are by g++. Still doesn't explain clang's weird behaviour here. A non-conforming implementation maybe?
clang
? The latest version prints const. – Printery--version
and add that result for both compilers? – Printerystd::pair
difference. – Innes