Can I use the "use" of ads with templates?

Can I use the using declaration with base classes of templates? I read that this is not here , but is it due to a technical reason or against the C ++ standard, and does it apply to gcc or other compilers? If this is not possible, why not?

Sample code (from the link above):

struct A {
    template<class T> void f(T);
};

struct B : A {
    using A::f<int>;
};
+5
source share
1 answer

What are you involved with is the using directive. Using the declaration can be used perfectly with the templates of the base classes (I did not look at it in the standard, but just tested it using the compiler):

template<typename T> struct c1 { 
    void foo() { std::cout << "empty" << std::endl; } 
}; 

template<typename T> struct c2 : c1<T> { 
    using c1<T>::foo; 
    void foo(int) { std::cout << "int" << std::endl; } 
}; 

int main() { 
    c2<void> c;
    c.foo();
    c.foo(10); 
}

foo , c2 .

: . :

, ( ). :

struct c1 { 
    template<int> void foo() { std::cout << "empty" << std::endl; } 
}; 

struct c2 : c1 { 
    using c1::foo; // using c1::foo<10> is not valid
    void foo(int) { std::cout << "int" << std::endl; } 
}; 

int main() { 
    c2 c;
    c.foo<10>();
    c.foo(10); 
}
+4

All Articles