lundi 20 août 2018

SFINAE method completely disables base class's template method in clang

#include <iostream>
#include <utility>

struct B {
    template<typename T, std::enable_if_t<std::is_same<T, int>::value>* = nullptr>
    void foo(T) {
        std::cout<<"B::foo"<<std::endl;
    }
};

struct D: B {        
    using B::foo;
    template<typename T, std::enable_if_t<std::is_same<T, std::string>::value>* = nullptr>
    void foo(T) {
        std::cout<<"D::foo"<<std::endl;
    }
};

int main() {
    D d;
    d.foo(2); // gcc: print "B::foo"; clang: compile error
    return 0;
}

Let's say we want to expose both foo() overloads in derived class D. gcc and Visual Studio compiles and print "B::foo" as I expected. But I get a compile error with clang:

prog.cc:22:7: error: no matching member function for call to 'foo'
    d.foo(2);
    ~~^~~
prog.cc:14:10: note: candidate template ignored: requirement 'std::is_same<int, std::string>::value' was not satisfied [with T = int]
    void foo(T) {

Is this a clang bug? Thanks!

Aucun commentaire:

Enregistrer un commentaire