I think you are not clear on how to use the type, float
, int
, etc., to grab an appropriate format string.
You can re-design your function to look something like:
template <typename type>
void d_prettyPrintVector(type* v, const unsigned int size)
{
printf("\n");
for(int i=0; i<size; i++)
printf(getFormatString<type>(), v[i]);
// ^^^ Get an format string appropriate for the type.
}
That would be perfectly valid code if you had a function template:
template <typename type> char const* getFormatString();
that had specializations for the types that you are interested in. In other words, the following should work:
template <typename type> char const* getFormatString();
template <typename type>
void d_prettyPrintVector(type* v, const unsigned int size)
{
printf("\n");
for(int i=0; i<size; i++)
printf(getFormatString<type>(), v[i]);
// ^^^ Get an format string appropriate for the type.
}
template <> char const* getFormatString<float>() { return "%4.1f "; }
template <> char const* getFormatString<int>() { return "%2d "; }
template <> char const* getFormatString<char>() { return "%2d "; }
template <> char const* getFormatString<bool>() { return "%1d "; }
Now, you can use:
int a[] = {1, 2};
d_prettyPrintVector(a, 2);
float b[] = {1.1f, 2.2f};
d_prettyPrintVector(b, 2);
without any problem.
Extra
You can extend that idea to provide a lambda function as an argument to d_prettyPrintVector
. The lambda function can return a custom format string that is more appropriate for a single use case.
Overload d_prettyPrintVector
. Provide one that can take a lamba function as an argument.
template <typename type, typename Format>
void d_prettyPrintVector(type* v, const unsigned int size, Format format)
{
printf("\n");
for(int i=0; i<size; i++)
printf(format(), v[i]);
}
You can even implement the initial function using the new function so you don't have to repeat the details of how the items are printed.
template <typename type> char const* getFormatString();
template <typename type>
void d_prettyPrintVector(type* v, const unsigned int size)
{
d_prettyPrintVector(v, size, [](){return getFormatString<type>();});
}
Now, in addition to the previous calls to print a
and b
, you can now use:
// Define the format on the fly, using a lambda function.
d_prettyPrintVector(a, 2, []() -> char const* {return "%4d ";});
// Define the format on the fly, using a lambda function.
d_prettyPrintVector(b, 2, []() -> char const* {return "%.6f ";});