In the following piece of code, I'm trying to build a lattice of types. For instance, between float
and int
, promote the result to float
:
float join(float f, int) { return f; }
float join(float f, float) { return f; }
Then I introduce a wrapper
type:
template <typename Inner>
struct wrapper
{
using inner_t = Inner;
inner_t value;
};
whose behavior with the join
operation quite natural:
template <typename Inner1, typename Inner2>
auto
join(const wrapper<Inner1>& w1, const wrapper<Inner2>& w2)
-> wrapper<decltype(join(w1.value, w2.value))>
{
return {join(w1.value, w2.value)};
}
It can also be join
ed with a "scalar" type:
template <typename Inner1, typename T2>
auto
join(const wrapper<Inner1>& w1, const T2& value2)
-> wrapper<decltype(join(w1.value, value2))>
{
return {join(w1.value, value2)};
}
So far, so good, it works. But then, because in the real case I actually have many more such rules, I'd like to avoid duplicating the number of rules to express the commutativity of the join
operation, and therefore, I express that join(scalar, wrapper) := join(wrapper, scalar)
(In fact, I'd prefer something like join(v1, v2) := join(v2, v1)
, but let's start with something more specific.):
template <typename T1, typename Inner2>
auto
join(const T1& value1, const wrapper<Inner2>& w2)
-> decltype(join(w2, value1))
{
return join(w2, value1);
}
this works properly for join(scalar, scalar)
, join(wrapper, scalar)
and join(scalar, wrapper)
. But then join(wrapper, wrapper)
results in an infinite expansions of template functions with both G++ 4.9 and Clang++ 3.5, which I don't understand.
int main()
{
int i;
float f;
wrapper<float> fr;
join(f, i);
join(fr, i);
join(i, fr);
join(fr, fr); // Loops.
}
Clang:
clang++-mp-3.5 -std=c++11 bar.cc
bar.cc:21:5: fatal error: recursive template instantiation exceeded maximum depth of
256
join(const wrapper<Inner1>& w1, const T2& value2)
^
bar.cc:29:5: note: while substituting deduced template arguments into function
template 'join' [with T1 = wrapper<float>, Inner2 = float]
join(const T1& value1, const wrapper<Inner2>& w2)
^
GCC:
g++-mp-4.9 -std=c++11 bar.cc
bar.cc:30:34: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) substituting 'template<class T1, class Inner2> decltype (join(w2, value1)) join(const T1&, const wrapper<Inner2>&) [with T1 = <missing>; Inner2 = <missing>]'
-> decltype(join(w2, value1))
^
bar.cc:30:34: recursively required by substitution of 'template<class T1, class Inner2> decltype (join(w2, value1)) join(const T1&, const wrapper<Inner2>&) [with T1 = wrapper<float>; Inner2 = float]'
bar.cc:30:34: required by substitution of 'template<class T1, class Inner2> decltype (join(w2, value1)) join(const T1&, const wrapper<Inner2>&) [with T1 = wrapper<float>; Inner2 = float]'
bar.cc:43:18: required from here
I do not understand why overloading does not cut the recursion. What is going on? There is probably a possible alternative implementation with (class) template specialization, but I'm not looking for alternative implementations: I'd like to understand what's wrong with this one. Thanks in advance.