国内优秀网站欣赏,建筑专业网站有哪些,上海百度地图,江阴网站建设C 什么时候用.template 和 ::template
简单来说#xff0c;就是你有一个未知类型T**#xff08;这个T本身就是模板#xff09;** 假设这个T是一个类#xff0c;这个类里包含了一些模板函数或者模板结构体 你需要使用T. 或者 T:: 去调用他们#xff0c;并且要显示指定模板…C 什么时候用.template 和 ::template
简单来说就是你有一个未知类型T**这个T本身就是模板** 假设这个T是一个类这个类里包含了一些模板函数或者模板结构体 你需要使用T. 或者 T:: 去调用他们并且要显示指定模板参数 这个时候就需要用到.template 和 ::template
如果上面文字描述没懂没关系看示例就懂了 编译环境: gcc/g 13.2 -stdc17 struct A {void Foo() { std::cout __PRETTY_FUNCTION__ std::endl; }template typename Tvoid Foo() {std::cout __PRETTY_FUNCTION__ std::endl;}template typename Tstatic void Goo() {std::cout __PRETTY_FUNCTION__ std::endl;}template typename Tstruct B {using type T;void Foo() { std::cout __PRETTY_FUNCTION__ std::endl; }template typename Uvoid Foo() {std::cout __PRETTY_FUNCTION__ std::endl;}};struct C {void Foo() { std::cout __PRETTY_FUNCTION__ std::endl; }};
};template typename T
struct Test {void Fun(T t) {t.Foo();t.template Fooint();t.template Gooint();T::template Gooint();typename T::template Bint::type a;typename T::template Bint().Foo();typename T::template Bint().template Fooint();typename T::C().Foo();}
};int main() {A a;TestA().Fun(a);
}编译输出内容
void A::Foo()
void A::Foo() [with T int]
static void A::Goo() [with T int]
static void A::Goo() [with T int]
void A::BT::Foo() [with T int]
void A::BT::Foo() [with U int; T int]
void A::C::Foo()