不明确的重载、隐式转换和显式构造函数
Ambiguous overloads, implicit conversion and explicit constructors
考虑以下小程序:
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {}
int f(const A& a) { return 0; }
int f(std::vector<int> a) { return 1; }
};
int g(const A& a) { return 2; }
int g(std::vector<int> a) { return 3; }
int main() {
A a(1,2);
// a.f({}); //ambiguous according to gcc
g({}); //ambiguous according to gcc
return 0;
}
GCC 10.2 拒绝编译它:它说对 g({})
和 a.f({})
的调用不明确。 Clang 毫无怨言地编译它。
在我看来,g(const A&)
不应在重载决策中考虑,因为不允许从无参数进行隐式转换:A::A()
被标记为显式。
我不确定问题不是我的,无论如何,我想找到解决方法。
是否有另一个默认生成的构造函数可能是我的问题的根源?
您可以在 compiler explorer 上试用。
这个 引起了我的注意。它的答案告诉我们哪个编译器是正确的:它是 GCC。但它没有告诉我们如何使用这些重载规则获得所需的行为。
你是对的,它看起来像是一个错误。由于应该没有歧义的确切原因,下面注释掉的行无法编译。
使用 std::initializer_list 为您找到解决方法:
#include <fmt/core.h>
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {fmt::print("Aab\n");}
void f(const A& a) { fmt::print("A\n"); }
void f(std::vector<int> a) { fmt::print("vector\n"); }
void f(std::initializer_list<int> l) {
return f(std::vector<int>(l));
}
};
void g(const A& a) { fmt::print("A\n"); }
void g(std::vector<int> a) { fmt::print("vector\n"); }
void g(std::initializer_list<int> a) {return g(std::vector<int>(a)); }
int main() {
A a(1,2);
A a2 = A();
//A a3 = {};
a.f({});
g({});
return 0;
}
考虑以下小程序:
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {}
int f(const A& a) { return 0; }
int f(std::vector<int> a) { return 1; }
};
int g(const A& a) { return 2; }
int g(std::vector<int> a) { return 3; }
int main() {
A a(1,2);
// a.f({}); //ambiguous according to gcc
g({}); //ambiguous according to gcc
return 0;
}
GCC 10.2 拒绝编译它:它说对 g({})
和 a.f({})
的调用不明确。 Clang 毫无怨言地编译它。
在我看来,g(const A&)
不应在重载决策中考虑,因为不允许从无参数进行隐式转换:A::A()
被标记为显式。
我不确定问题不是我的,无论如何,我想找到解决方法。
是否有另一个默认生成的构造函数可能是我的问题的根源?
您可以在 compiler explorer 上试用。
这个
你是对的,它看起来像是一个错误。由于应该没有歧义的确切原因,下面注释掉的行无法编译。
使用 std::initializer_list 为您找到解决方法:
#include <fmt/core.h>
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {fmt::print("Aab\n");}
void f(const A& a) { fmt::print("A\n"); }
void f(std::vector<int> a) { fmt::print("vector\n"); }
void f(std::initializer_list<int> l) {
return f(std::vector<int>(l));
}
};
void g(const A& a) { fmt::print("A\n"); }
void g(std::vector<int> a) { fmt::print("vector\n"); }
void g(std::initializer_list<int> a) {return g(std::vector<int>(a)); }
int main() {
A a(1,2);
A a2 = A();
//A a3 = {};
a.f({});
g({});
return 0;
}