在一篇文章The case for D我看到了这个 C++ 表达式:
object.template fun<arg>()
谁能解释一下?
编辑:谢谢马克,我现在看到确实是重复的。但由于它只是一个更全面的答案中的一个旁注,我将在一个例子中浓缩具体的答案。
#include <iostream>
using namespace std;
template <typename T>
struct A
{
T y;
void write()
{
y.T::template print<int>(); // GOOD: I have seen often
y.template print<int>(); // GOOD: shortcut, I did not know
y.print<int>(); // ERROR: I have seen this work on old compiler
}
};
struct B
{
template <typename T>
void print() {cout << "print b" << endl;}
};
int main ()
{
A<B> a;
a.write();
}