Implicit type conversion with template
Solution 1
The solution is already shown in this answer. Now, more about the problem...
The problem in your code is how overload resolution is performed. When a template function is considered for overload resolution the compiler will perform type deduction on the arguments and come up with a type substitution that matches the call or else it fails to apply that template, removes it from the set of potential candidates and continues over. The problem at this point is that type deduction only deduces exact matches (with possible extra const/volatile qualification). Because the matching is exact, the compiler will not use any conversion (again, other than cv).
The simplest example of this happens with std::max
and std::min
functions:
unsigned int i = 0;
std::min( i, 10 ); // Error!
Type deduction will deduce the T in template <typename T> min( T const &, T const & )
to be unsigned
for the first argument but int
for the second they differ and the compiler will discard this template function.
The solution proposed in the answer is using a feature of the language that enables you to define a non-member friend function inside the class definition. The advantage with templates is that for every (different) instantiation of the template, the compiler will create a free non-template function at namespace level that has the signature obtained by substituting the real types of the instantiation in the friend declaration:
template <typename T>
class test {
friend test operator+( test const & lhs, test const & rhs ) { // [1]
return test();
}
}
test<int> t; // [2]
In the example above, the compiler allows you to add the definition of the friend function inside the class scope at [1]. Then when you instantiate the template in [2], the compiler will generate a free function:
test<int> operator+( test<int> const & lhs, test<int> const & rhs ) {
return test<int>();
}
The function is defined always, whether you use it or not (this differs to the template class member functions, that are instantiated on demand).
The magic here has multiple sides to it. The first part is that it generically you are defining non-template functions for each and all of the instantiated types, so you gain genericity and at the same time the advantage of overload resolution being able to use this function when the arguments are not perfect matches.
Because it is a non-template function, the compiler is able to call implicit conversions on both arguments, and you will get your expected behavior.
Additionally, a different type of magic goes on with lookup, as the function so defined can only be found by argument dependent lookup unless it is also declared at namespace level, which in our case cannot be done in a generic way. The implication of this might be good or bad, depending on how you want to consider it...
Because it can only be found by ADL it will not be considered unless at least one of the arguments is already of the desired type (i.e. it will never be used performing conversions to both arguments). The downside is that it is impossible to refer to the function unless you are actually calling it, and that means that you cannot obtain a function pointer.
(More on template friendship here, but note that in this particular case, all the other variants will fail to perform implicit conversions).
Solution 2
Every attempt to provide an operator using templates will need at least one second overload. But you can avoid that by defining the operator inside the class:
template <unsigned int m>
class A
{
public:
A(int) {}
inline friend A operator+(const A& a, const A& b) { return A(0); }
};
Works for both, a+5
and 5+a
.
Solution 3
Add this operator
template<unsigned int m>
A<m> operator+(const A<m>&, const int&)
{
return A<m>(0);
}
OR try this
template <unsigned int m>
class A
{
friend const A operator+(const A& a, const A& b) { return A(0); }
public:
A(int) {}
// OR FOR UNARY
// const A operator+(const A &a) const {return A(0);}
};
int main(){
A<3> a(4);
A<3> b = a + 5;
A<3> c = 5 + a;
}
Seagull
Updated on July 10, 2022Comments
-
Seagull almost 2 years
I have a template
class A
template <unsigned int m> class A { public: A(int) {} };
Which has a constructor from
int
. And I have an operation:template<unsigned int m> A<m> operator+(const A<m>&, const A<m>&) { return A<m>(0); }
But when I call:
A<3> a(4); A<3> b = a + 5; A<3> c = 5 + a;
I would like
int
to be implicitly converted to A, but compilers throws error.Is there any elegant way to enable implicit conversion without using such solutions as:
a + A<m>(5)
operator+<3>(a, 5)
-
Seagull about 12 yearsIt's not an elegant solution as I also should add
A<m> operator+( const int&, const A<m>&)
and do so for all other operations. -
Dmitriy Kachko about 12 yearswatch my addition it the answer I don't know is it elegant though :).
-
Seth Carnegie about 12 yearsYou will also need an overload for
5 + a
. -
Seagull about 12 yearsThanks this solution is much better, but something should be created for
5+a
-
Dmitriy Kachko about 12 yearsspecify it in your question, please
-
Matthieu M. about 12 years@TimKachko: It is fairly common for a binary operator to be symmetric (and usually expected) so while not specified in the question, it would be courteous to provide the answer.
-
David Rodríguez - dribeas about 12 years+1, A bit more explaining might be appropriate for those that don't know the reason.
-
Seagull about 12 yearsBut I need template
A<m>
, as I would like to forbide operation for objects with differentm
. -
David Rodríguez - dribeas about 12 years@Seagull: The
A
with no template parameters inside the definition of the template classA
refers toA<m>
not just anyA<x>
. -
David Rodríguez - dribeas about 12 years@Seagull, I have provided a much longer explanation of what is actually going on and why this is the solution as a different answer here.
-
onitake over 9 yearsIs there a way to define this operator overload out of class and only have a "special" friend declaration in the class body? I haven't found a way to do so.
-
David Rodríguez - dribeas over 9 years@onitake: I don't quite understand what you are asking
-
onitake over 9 years@dribeas: I usually prefer separating declaration and implementation. So, if I want to only put the friend declaration into the class, but have the definition of the function elsewhere, is there a way to do this?
-
David Rodríguez - dribeas about 7 years@onitake: Kind of late, but not even 3 years... You cannot define that friend function externally as it is a different function for each instantiation of the template. What you can do to avoid adding much code there is to provide a function that does the actual work (say
static test add(test const&, test const&)
) in the class and have the definition of the friend just forward:test operator+(test const& a, test const& b) { return add(a,b); }
. Not perfect but at least the definition in the class is a single line. The more complex logic is hidden intest<T>::add
. -
Nir Friedman over 6 years@DavidRodríguez-dribeas This answer isn't quite correct. It says the match has to be exact "with possible extra const/volatile qualification". However, derived to base, as well as certain pointer qualification conversions are allowed too (e.g.
int *
toint const *
). en.cppreference.com/w/cpp/language/template_argument_deduction. Happy to edit if you agree. -
Ad N almost 3 yearsThis gives a great understanding of the situation, thank you @DavidRodríguez-dribeas! Now, I am doing a bit of necromancy wondering if there is a solution able to also handle conversions on both arguments? (Or does it require to write the explicit overload with both arguments as
A
?) -
Ad N almost 3 yearsAnswering to myself: it does work in my environment when both types are
A
(VS 16.10.13, compiling for C++17).