在C++ Primer Plus(2001,捷克语翻译)中,我发现了这些不同的模板特化语法:
功能模板
template <typename T> void foo(T);
Run Code Online (Sandbox Code Playgroud)
专业化语法
void foo(int param); // 1
void foo<int>(int param); // 2
template <> void foo<int>(int param); // 3
template <> void foo(int param); // 4
template void foo(int param); // 5
Run Code Online (Sandbox Code Playgroud)
谷歌搜索了一下,我发现只有3号例子.它们之间是否存在差异(通话,编译,使用)?其中一些是否已过时/弃用?为什么不使用No.1?
我有一个带有模板化函数的类。该函数是专门化/实例化的,因此可以在 cpp 中定义。Doxygen 给了我一个关于模板实例化的错误。
例如,我的.h:
namespace LP
{
namespace LF
{
class FileReader
{
public:
template <class T> void Read( T *aValue );
size_t Read( uint8_t *aBuffer, size_t aSizeToRead );
};
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的 cpp:
/// Valid doxygen function doc
template<class T>
void
LP::LF::FileReader::Read( T *aValue )
{
Read( reinterpret_cast<uint8_t *>( aValue ), sizeof( T ) );
}
//Template specialisation so it can be defined in the cpp file
template void LP::LF::FileReader::Read<uint8_t>( uint8_t * );
template void LP::LF::FileReader::Read<uint16_t>( uint16_t * …Run Code Online (Sandbox Code Playgroud)