Syd*_*ius 7 c++ gcc templates using
是否可以将"using"声明与模板基类一起使用?我已经读过它不在这里,但是由于技术原因还是它违反C++标准,它是否适用于gcc或其他编译器?如果不可能,为什么不呢?
示例代码(来自上面的链接):
struct A {
template<class T> void f(T);
};
struct B : A {
using A::f<int>;
};
Run Code Online (Sandbox Code Playgroud)
你链接的是using指令.using声明可以与模板化的基类一起使用(在标准中没有查找它,但只是用编译器测试它):
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);
}
Run Code Online (Sandbox Code Playgroud)
编译器正确地找到无参数foo函数,因为我们的using声明将其重新声明为范围c2,并输出预期结果.
编辑:更新了问题.这是更新的答案:
文章是正确的,你不允许使用template-id(模板名称和参数).但是你可以放一个模板名称:
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);
}
Run Code Online (Sandbox Code Playgroud)