为什么使用格式错误的函数而不是隐式转换?

Jak*_*dle 5 c++ ambiguity malformed overload-resolution c++11

这是我的代码段:

class Base{};

class Derived : private Base{};

template<class T>
class Wrapper
{
    public:
        template<typename T2>
        Wrapper( T2&& )
        { }
};

// Function declarations
void func( Base& param );
void func( Wrapper<Derived> );
void funcUnambiguous( Wrapper<Derived> );


// Here is the Call:
Derived d = Derived();  
func( d );               // <- Error
Run Code Online (Sandbox Code Playgroud)

GCC 4.9给了我: error: 'Base' is an inaccessible base of 'Derived'

而我这样做

Derived d = Derived();

funcUnambiguous( d );
Run Code Online (Sandbox Code Playgroud)

它只是工作正常.

看起来,任何只需要廉价演员的功能,即使是格式错误,也隐藏着隐含但昂贵的演员功能.有人有线索吗?

Dan*_*rey 5

更新了,感谢@TC

Wrapperctor是模板 用户定义的转换,因此非模板 标准转换序列重载Base&优先.访问检查仅选择过载执行- 在您的情况下为时已晚.

完整的规则很复杂,更多可以在这里找到,请看"最佳可行功能"部分.

  • 它不是模板与非模板; 那个决胜局无论如何也无法区分两个`func`,因为它们都不是模板.派生到基础转换被认为是标准转换序列,它胜过用户定义的转换序列. (2认同)