1

I want to support a global binary operator on the inner class of a template class:

template <unsigned N, typename T>
class Parent
{
public:

    class Child
    {
    public:
        Child & operator |= (const Child &);
    };
};

template <unsigned N, typename T>
typename Parent<N,T>::Child
operator | (
    const typename Parent<N,T>::Child & a,
    const typename Parent<N,T>::Child & b)
{
    return Parent<N,T>::Child (a) |= b;
}

int main ()
{
    Parent<1,char>::Child a;
    Parent<1,char>::Child b;

    auto c = a | b;
}

To my eyes, the expression a|b should have no trouble agreeing with the operator| prototype, but (gcc 7.3.0):

error: no match for ‘operator|’ (operand types are ‘Parent<1, char>::Child’ and ‘Parent<1, char>::Child’)
...
template argument deduction/substitution failed:
couldn't deduce template parameter ‘N’

Note that this works:

auto c = Parent<1,char>::Child(a) |= b

Why doesn't the global operator work?

spraff
  • 32,570
  • 22
  • 121
  • 229
  • 1
    https://stackoverflow.com/questions/48387973/template-parameter-cannot-be-deduced/48388193#48388193 – llllllllll Nov 27 '18 at 12:39
  • Make that operator member of that class or friend function. – Jarod42 Nov 27 '18 at 13:02
  • It probably doesn't address the problem you see, but there's an obvious simplification: if you were to pass `a` by value, you'd no longer need to make your own copy. – Toby Speight Dec 06 '18 at 12:47

0 Answers0