#include <iostream> #include "reflection.hpp" int i = 0; class A { public: A() { std::cout << "ctor of A" << std::endl; } }; class B : public A { public: B() { std::cout << "ctor of B" << std::endl; } }; template <typename Base, typename Derived> class C { public: C() { std::cout << "ctor of C" << std::endl; } template <typename D> int func() { D* gD = new D; delete gD; std::cout << "func of C" << std::endl; return 0; } }; template <typename Base, typename Derived> class D { public: D() { } int func() { C<Base, Derived> c0; [color=#FF0000] c0.func<Derived>();[/color] return 0; } }; int main(void) { std::cout << i << std::endl; C<A, B> c0; B b0; c0.func<B>(); D<A, B> d0; d0.func(); return 0; } 使用环境为Codeblocks10.05-gcc4.4.1。出错信息如下: test_template.cpp||In member function 'int D<Base, Derived>::func()':| test_template.cpp|37|error: expected primary-expression before '>' token| test_template.cpp|37|error: expected primary-expression before ')' token| 原因: GCC对C++语法检查严格。使用c0.func<Derived>()调用不正确。应使用:c0.template func<Derived>()~~