mardi 3 novembre 2015

Possible compiler bug? auto deduction isn't possible when calling a templated method with a pointer retrieved from a templated static member function

Live example

Consider the following class:

struct A {
    template<bool>
    bool is() const {
        return true;
    }

    template<bool>
    static A* get() {
        static A a;
        return &a;
    }
};

If a template function, such as

template<bool cond>
bool foo() {
    auto p = A::get<cond>();
    return p->is<true>();
}

tries to call A::is<bool>, it fails miserably:

main.cpp: In function 'bool foo()':
main.cpp:17:24: error: expected primary-expression before ')' token
     return p->is<true>();
                        ^
main.cpp: In instantiation of 'bool foo() [with bool cond = true]':
main.cpp:21:22:   required from here
main.cpp:17:17: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
     return p->is<true>();
                 ^

however, if only we replace auto with the explicit type (A*):

template<bool cond>
bool foo() {
    A* p = A::get<cond>();
    return p->is<true>();
}

it works.

What is the reason for this?

Aucun commentaire:

Enregistrer un commentaire