为何编译器会认为此构造函数重载解析度不明确?

时间:2018-10-11 19:39:58

标签: c++ constructor implicit-conversion

我不知道为什么编译器(Visual Studio 2017)认为这种半递归重载解决方案不明确,并且需要强制转换:

class A
{
public:
    A(int a) {}
};

class B
{
public:
    B(A a) {}
};

void func(B b) {}

void useCase()
{
    func((B)1);   // OK! So it "knows" how to convert from 1 to B 
    func(1);      // ERROR: why is it ambiguous to convert 1, when no alternative exists?
    func((int)1); // ERROR: why is it ambiguous to convert 1, when no alternative exists?
}

错误是: 错误C2664:'void func(B)':无法将参数1从'int'转换为'B' 注意:没有构造函数可以采用源类型,或者构造函数重载解析不明确

我可以解决此问题,并且可以肯定我遗漏了一些明显的内容,但是我很想知道发生了什么。 ;)

0 个答案:

没有答案
相关问题