为什么以这种方式在C 中定义模板函数
Why define template function in C++ this way?
#include <openpose/utilities/errorAndLog.hpp>
#include <openpose/utilities/string.hpp>
namespace op
{
template<typename T>
std::string toFixedLengthString(const T number, const unsigned long long stringLength)
{
try
{
const auto numberAsString = std::to_string(number);
if (stringLength > 0)
{
if (number < 0)
error("toFixedLengthString: number cannot be <= 0, in this case it is: " + numberAsString + ".", __LINE__, __FUNCTION__, __FILE__);
const auto zerosToAdd = stringLength - numberAsString.size();
if (zerosToAdd < 0)
{
const auto errorMessage = "toFixedLengthString: number greater than maximum number of digits (stringLength): "
+ numberAsString + " vs. " + std::to_string(stringLength) + ".";
error(errorMessage, __LINE__, __FUNCTION__, __FILE__);
}
return { std::string(zerosToAdd, '0') + numberAsString};
}
else
return numberAsString;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return "";
}
}
// Signed
template std::string toFixedLengthString<char>(const char number, const unsigned long long stringLength);
template std::string toFixedLengthString<signed char>(const signed char number, const unsigned long long stringLength);
template std::string toFixedLengthString<short>(const short number, const unsigned long long stringLength);
template std::string toFixedLengthString<int>(const int number, const unsigned long long stringLength);
template std::string toFixedLengthString<long>(const long number, const unsigned long long stringLength);
template std::string toFixedLengthString<long long>(const long long number, const unsigned long long stringLength);
// Unsigned
template std::string toFixedLengthString<unsigned char>(const unsigned char number, const unsigned long long stringLength);
template std::string toFixedLengthString<unsigned short>(const unsigned short number, const unsigned long long stringLength);
template std::string toFixedLengthString<unsigned int>(const unsigned int number, const unsigned long long stringLength);
template std::string toFixedLengthString<unsigned long>(const unsigned long number, const unsigned long long stringLength);
template std::string toFixedLengthString<unsigned long long>(const unsigned long long number, const unsigned long long stringLength);
}
这是SRC文件,他在标题文件中具有相应的功能。他为什么需要定义下面的内容,而不仅仅是将整个模板函数放在标题文件中,根据So的其他帖子,应该是人们定义模板函数的方式。
(我是开放式作者之一(
主要原因:如果我在HPP中定义它,那么每次您对代码进行少量更改时,您都必须等待很多时间来编译代码的所有模板。
以这种方式,这些模板仅是一次汇编一次(您第一次编译possose(,不是每次更改代码时。
旁注:我接受建议和评论,当我不像C 中的专家时,我自己的代码太多,无法进行openpose。
如果未定义的方式未定义,链接器将丢弃错误。替代选项是将所有功能定义添加到标题文件中。
请参阅:https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-defn-from-decl
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数