Reputation: 10153
I have a function :
template<class T>
static string
format(T ui, T sentinal, char listSeparator)
{
stringstream s;
if (ui == sentinal)
{
s << "n/a" << listSeparator;
}
else
{
s << ui << listSeparator;
}
return s.str();
}
The way the function was called is :
output << format(field1,Backend::NA_Value, csvSeparator);
output << format(field2,Backend::NA_Value, csvSeparator);
/// ...etc
Previously field1
and field2
were of the type unsigned int
.
It was decided to change these types to be unsigned long long
.
But the compilation error occurs :
std::string format(T,T,char)' : template parameter 'T' is ambiguous
main.cpp(39) : see declaration of 'format'
could be 'Juint'
'unsigned __int64'
What is the reason for that?NA_Value?It is defined as :
static const Juint NA_Value = (Juint) -1;
typedef unsigned int Juint
It can`t determine the template T ?! Where from compiler decides about __int64?
Upvotes: 2
Views: 300
Reputation: 45725
This is a problem of template type deduction, where you have the same type occurring multiple times in the signature of a function. The compiler just doesn't know which of the two types you want T
to be, because in the call the two occurrences are of a different type.
You call format
like the following (with Juint
being replaced according to your typedef
):
format(unsigned long long ui, unsigned int sentinal, char listSeparator);
You changed the type of the former argument so now it is different from the second. Before you did this change, both parameters had the same type.
To solve the problem, you have the following options:
Make the template types different. (see answer by Joachim Pileborg)
Force a specific type by explicitly stating it as a template parameter when you call the function. This will automatically cast the non-matching parameter(s) like you are used to it for normal functions (no type deduction happens in this case):
output << format<Juint>(field2,Backend::NA_Value, csvSeparator);
^^^^^^^
output << format<unsigned long long>(field2,Backend::NA_Value, csvSeparator);
^^^^^^^^^^^^^^^^^^^^
Cast one of the parameters to the other type. This makes both parameters be the same and type deduction will succeed:
output << format(static_cast<Juint>(field2),Backend::NA_Value, csvSeparator);
^^^^^^^^^^^^^^^^^^
output << format(field2,static_cast<unsigned long long>(Backend::NA_Value), csvSeparator);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Change the typedef of Juint
, too, so the types become the same again and type deduction also succeeds.
Add type traits / enable_if
which will restrict the template types to specific classes of types fulfilling a condition, but in your case I don't think that this is what you want.
Personally, I'd wish there was another option: It would be nice to have the type deduction only perform on the first parameter and force the second parameter to be cast on the call site automatically (like a "T
but don't deduce" parameter type). But there isn't such an option.
The best options in your case is to make Juint
also of the same type (4th option on the list) or to make your function "more general" by going for the first option. Care has to be taken on the usages of the types within the function, as pointed out in the answer by Joachim Pileborg in general, but in your case it should be fine without changing the function's body.
Upvotes: 3
Reputation: 15768
The compiler error says it all: What type is T
meant to be? Is it unsigned long long
as indicated by field1
, or it it unsigned int
, as indicated by Backend::NA_Value
?
The compiler can't determine what you meant, so it produces an error.
Possible solutions are:
Backend::NA_Value
to unsigned long long
as wellExplicitly specify the type you want to be used for T
:
format<unsigned long long>(field1, Backend::NA_Value, csvSeparator);
Change format such that the first and second argument can be of different types:
template<class T, class U>
static string
format(T ui, U sentinal, char listSeparator)
{
stringstream s;
if (ui == sentinal)
{
s << "n/a" << listSeparator;
}
else
{
s << ui << listSeparator;
}
return s.str();
}
With this change, sentinal
can be of any type that can be equality-compared with ui
.
Upvotes: 1
Reputation: 409442
The types for Backend::NA_Value
is no longer the same as the field1
or field2
variables. One is a unsigned int
while the other is a unsigned long long
.
Either change the Juint
typedef to match the type of field1
and field2
. Or you can add a new template variable:
template<class T, class U>
static string
format(T ui, U sentinal, char listSeparator)
Of course if you do the latter you will get other errors and/or warning about mismatching types in the comparison.
Upvotes: 1