奇怪的C ++模板语法

时间:2013-08-30 02:33:59

标签: c++ templates syntax

在文章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();
}

0 个答案:

没有答案
相关问题