编译器之间的重载分辨率不同
Overload Resolution differs between compilers
我构建了以下我的问题的最小示例:
#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);
}
在我的 Ubuntu 机器上 g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 将打印出以下内容:
$ g++ -std=c++14 test.cpp -o test && ./test
default
const
const
但是,Apple clang(版本 11.0.3 (clang-1103.0.32.62)
目标:x86_64-apple-darwin19.4.0) 在我的 Mac 上将打印:
$ g++ -std=c++14 test.cpp -o test && ./test
default
const
non-const
然而,情况变得更糟:如果我将最后一行更改为
std::pair<Foo, int>foop1(foop0);
^ removed const
两个编译器都会给出第一个输出。
为什么会这样?
编辑:我现在明白了为什么,根据 cppreference,std::pair 的 ctors 应该按原样由 g++ 选择。这里仍然没有解释 clang 的怪异行为。可能是不符合规范的实施?
如前所述,可能 std::pair
两者的实现不同。我写了两个非常相似的一对实现,它们完全展示了你的不同行为,即使没有改变对的类型:godbolt
#include <iostream>
struct T {
T() {
std::cerr << "default\n";
}
T(T&) {
std::cerr << "non-const\n";
}
T(const T&) {
std::cerr << "const\n";
}
};
// Comment or uncomment to change the behavior.
//#define MAC
template<class First, class Second>
struct pair {
First first;
Second second;
pair(const First& f, const Second& s) : first(f), second(s) {
}
#ifdef MAC
pair(pair<First, Second>& p) : first(p.first), second(p.second) {
std::cerr << "copy Mac-Like\n";
}
#else
pair( pair<First, Second>& p) : pair(p.first, p.second) {
std::cerr << "copy Ubuntu-Like\n";
}
#endif
};
int main() {
T t;
pair<T, int> u1(t, 0);
pair<T, int> u2(u1);
}
当然,mac 和 ubuntu 上的对都写得更合理(并且符合标准)并且标准复制构造函数采用 const 引用(这是然后他们都使用 const 变体的原因)。但我猜他们处理具有不同但可转换类型的对的复制构造函数的方式不同。找出究竟有什么不同需要比较两个系统上的 stl 实现。
Ubuntu 变体对我来说似乎很清楚,这对只是通过构造函数中的 const 引用从一对可转换类型中获取的。当你在构造链的任何一点都有一个 const 时,你将以 T
的 const 复制构造函数结束。
我发现 Mac 行为有点奇怪,因为它们必须按值或非常量引用获取对(实际上,你不应该有一个通过非常量引用获取的复制构造函数,它为什么要改变它复制的东西?这看起来像是 std::auto_ptr
级的怪异)。也许他们(试图)对某种 "take it by value and then move" 事情很聪明。
但我认为这是不合格的,因为 pair constructor 应该通过常量引用或右值引用来获取所有其他对。由于我们正在复制,它应该使用复制构造函数,采用 const 引用,因此也有对 pair.first
的 const 引用,并由此采用其 const 复制构造函数。
我构建了以下我的问题的最小示例:
#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);
}
在我的 Ubuntu 机器上 g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 将打印出以下内容:
$ g++ -std=c++14 test.cpp -o test && ./test
default
const
const
但是,Apple clang(版本 11.0.3 (clang-1103.0.32.62) 目标:x86_64-apple-darwin19.4.0) 在我的 Mac 上将打印:
$ g++ -std=c++14 test.cpp -o test && ./test
default
const
non-const
然而,情况变得更糟:如果我将最后一行更改为
std::pair<Foo, int>foop1(foop0);
^ removed const
两个编译器都会给出第一个输出。
为什么会这样?
编辑:我现在明白了为什么,根据 cppreference,std::pair 的 ctors 应该按原样由 g++ 选择。这里仍然没有解释 clang 的怪异行为。可能是不符合规范的实施?
如前所述,可能 std::pair
两者的实现不同。我写了两个非常相似的一对实现,它们完全展示了你的不同行为,即使没有改变对的类型:godbolt
#include <iostream>
struct T {
T() {
std::cerr << "default\n";
}
T(T&) {
std::cerr << "non-const\n";
}
T(const T&) {
std::cerr << "const\n";
}
};
// Comment or uncomment to change the behavior.
//#define MAC
template<class First, class Second>
struct pair {
First first;
Second second;
pair(const First& f, const Second& s) : first(f), second(s) {
}
#ifdef MAC
pair(pair<First, Second>& p) : first(p.first), second(p.second) {
std::cerr << "copy Mac-Like\n";
}
#else
pair( pair<First, Second>& p) : pair(p.first, p.second) {
std::cerr << "copy Ubuntu-Like\n";
}
#endif
};
int main() {
T t;
pair<T, int> u1(t, 0);
pair<T, int> u2(u1);
}
当然,mac 和 ubuntu 上的对都写得更合理(并且符合标准)并且标准复制构造函数采用 const 引用(这是然后他们都使用 const 变体的原因)。但我猜他们处理具有不同但可转换类型的对的复制构造函数的方式不同。找出究竟有什么不同需要比较两个系统上的 stl 实现。
Ubuntu 变体对我来说似乎很清楚,这对只是通过构造函数中的 const 引用从一对可转换类型中获取的。当你在构造链的任何一点都有一个 const 时,你将以 T
的 const 复制构造函数结束。
我发现 Mac 行为有点奇怪,因为它们必须按值或非常量引用获取对(实际上,你不应该有一个通过非常量引用获取的复制构造函数,它为什么要改变它复制的东西?这看起来像是 std::auto_ptr
级的怪异)。也许他们(试图)对某种 "take it by value and then move" 事情很聪明。
但我认为这是不合格的,因为 pair constructor 应该通过常量引用或右值引用来获取所有其他对。由于我们正在复制,它应该使用复制构造函数,采用 const 引用,因此也有对 pair.first
的 const 引用,并由此采用其 const 复制构造函数。