我正在尝试将我自己的lib从Visual Studio移植到GNU/Linux上的g ++,我在模板编译时遇到了一些问题.实际上,在Visual C++中,模板只有在代码中明确使用时才会生成,而看起来(根据我的错误)g ++在首次使用之前会评估模板的内容.这会导致以下错误:
error: incomplete type ‘X’ used in nested name specifier
Run Code Online (Sandbox Code Playgroud)
...因为我在模板代码之后包含了一些类,而不是之前.我这样做是因为交叉使用冲突.
总而言之,Visual C++似乎不会尝试在使用时解析模板的内容,而g ++会尽快解析.
class MyClass;
template<class _Ty>
void func(MyClass* a_pArg)
{
a_pArg->foo();
};
Run Code Online (Sandbox Code Playgroud)
(_Ty没有使用,但没关系,只是解释问题)
在那种情况下,Visual C++将编译(即使MyClass不是预先声明的),而g ++不会,因为MyClass尚未完全声明.
有没有办法告诉g ++只在使用时实例化模板?