【发布时间】:2020-05-31 22:04:29
【问题描述】:
我构建了以下问题的最小示例:
#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) Target: 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 的 ctor 应该由 g++ 选择。仍然没有在这里解释 clang 的奇怪行为。可能是不合格的实现?
【问题讨论】:
-
他们是否使用相同版本的 C 标准?如果添加开关来指定标准版本会发生什么?
-
clang是什么版本?最新的version 打印 const。 -
@EricPostpischil 在主干上,所有 c++ 版本在 gcc 和 clang 上打印 const。至少从 c++11 开始。
-
两者都是 C++14。我现在编辑了问题。
-
你也可以运行
--version并为两个编译器添加该结果吗?
标签: c++ compilation overload-resolution