代码之家  ›  专栏  ›  技术社区  ›  Matthew

为什么这种类型转换不起作用?

c++
  •  2
  • Matthew  · 技术社区  · 6 年前

    考虑下面的代码( live demo ):

    class Foo
    {
    public:
        void method();
    };
    
    template <typename T> struct Member;
    template <typename T> struct Member<void (T::*)()>
    {
        using Object = T;
    };
    
    template <typename T> class Pointer
    {
    public:
        operator T*();
    };
    
    template <typename Func>
    void bar(typename Member<Func>::Object*, Func) {}
    
    void test()
    {
        Pointer<Foo> p;
        bar(p, &Foo::method);
    }
    

    这是使用GCC 4.9编译的,但GCC 4.8不知道如何调用转换运算符:

    error: no matching function for call to 'bar(Pointer<Foo>&, void (Foo::*)())'
         bar(p, &Foo::method);
    note: candidate is:
    note: template<class Func> void bar(typename Member<Func>::Object*, Func) void bar(typename Member<Func>::Object*, Func) {}
    note:   template argument deduction/substitution failed:
    note:   mismatched types 'typename Member<Func>::Object*' and 'Pointer<Foo>'
         bar(p, &Foo::method);
    

    为什么?是虫子吗?这是C++14中的新功能吗?如果可能的话,请引用C++特性或GCC错误报告。

    谢谢

    (注意:我有一个真正的代码解决方案,我只是想知道编译器中发生了什么变化…)

    0 回复  |  直到 6 年前