-2
#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);
}

This is the src file and he has the corresponding function in the header file. Why does he need to define the thing below and not just put the entire template function in the header file, which according to other posts on SO, should be how people define template function.

Tas
  • 7,023
  • 3
  • 36
  • 51
user10024395
  • 1
  • 1
  • 3
  • 20
  • You don't _need_ to define it that way. It would make more sense for it to be in the header file. It _can_ be defined that way, but it means you need to provide an instantiation for every use of it. – Tas Jun 07 '17 at 02:17
  • See https://stackoverflow.com/questions/2351148/explicit-instantiation-when-is-it-used and https://stackoverflow.com/questions/13068993/when-would-you-use-template-explicit-instantiation – Curious Jun 07 '17 at 02:27

2 Answers2

0

(I am one of the OpenPose authors)

Main reason: If I define it in the hpp, then everytime you make a small change in your code you will have to wait a lot of time to compile all the templates for your code.

In this way, these templates are compiled only once (the first time you compile OpenPose), not every time you make a change in your code.

Side note: I accept suggestions and comments, too many lines of code by myself for OpenPose when I am not as expert as I would like in C++ yet.

Ginés Hidalgo
  • 717
  • 9
  • 20
0

If it's not defined that way, the linker will throw an error. The alternative option is to add all the function definitions to the header file.

See: https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl

James
  • 270
  • 1
  • 10