Consider the template dump
function below:
namespace {
using namespace Eigen;
using namespace std;
using namespace vMAT;
template <typename T>
NSString *
dump(NSString * prefix, T * A, vMAT_Size sizeA)
{
NSMutableString * dump = [NSMutableString stringWithString:prefix];
[dump appendString:@" = \n"];
Eigen::Map<Matrix<T, Dynamic, Dynamic>> DATA(A, sizeA[0], sizeA[1]);
stringstream out;
out << DATA << endl;
[dump appendFormat:@"%s", out.str().c_str()];
return dump;
}
template <> // Specialized so elements print as numbers instead of chars
NSString *
dump(NSString * prefix, int8_t * A, vMAT_Size sizeA)
{
NSMutableString * dump = [NSMutableString stringWithString:prefix];
[dump appendString:@" = \n"];
Eigen::Map<Matrix<int8_t, Dynamic, Dynamic>> DATA(A, sizeA[0], sizeA[1]);
stringstream out;
out << DATA.cast<int32_t>() << endl;
[dump appendFormat:@"%s", out.str().c_str()];
return dump;
}
template <> // Specialized so elements print as numbers instead of chars
NSString *
dump(NSString * prefix, uint8_t * A, vMAT_Size sizeA)
{
NSMutableString * dump = [NSMutableString stringWithString:prefix];
[dump appendString:@" = \n"];
Eigen::Map<Matrix<uint8_t, Dynamic, Dynamic>> DATA(A, sizeA[0], sizeA[1]);
stringstream out;
out << DATA.cast<uint32_t>() << endl;
[dump appendFormat:@"%s", out.str().c_str()];
return dump;
}
}
As you can see, I've copy/paste/edited it to specialize how int8_t
and uint8_t
matrices are dumped. But this is exactly the sort of madness that templates are supposed to eliminate!
I have tried to add an additional template typename AsT
parameter to the original function, but keep running afoul of the compiler complaining about this line:
out << DATA.cast<AsT>() << endl;
Xcode complains that the cast<AsT>()
is a "dependent template" and wants to insert the template
keyword in front of it… Which seems to be nonsensical syntax that then generates yet another compiler error.
What is a better way to specialize this template function for int8_t and uint8_t?