12

I've encountered some code which I think should compile, but doesn't. So I'm hoping some of the local standards experts here at SO can help :-).

I basically have some code which resembles this:

#include <iostream>

template <class T = int>
class A {
public:
    class U {
    };

public:
    U f() const { return U(); }
};

// test either the work around or the code I want...
#ifndef USE_FIX
template <class T>
bool operator==(const typename A<T>::U &x, int y) {
    return true;
}
#else
typedef A<int> AI;
bool operator==(const AI::U &x, int y) {
    return true;
}
#endif

int main() {
    A<int> a;
    std::cout << (a.f() == 1) << std::endl;
}

So, to describe what is going on here. I have a class template (A) which has an internal class (U) and at least one member function which can return an instance of that internal class (f()).

Then I am attempting to create an operator== function which compares this internal type to some other type (in this case an int, but it doesn't seem to matter).

When USE_FIX is not defined I get the following error:

test.cc: In function 'int main()':
test.cc:27:25: error: no match for 'operator==' in 'a.A<T>::f [with T = int]() == 1'

Which seems odd, because I am clearly (I think) defining a templated operator== which should cover this, in fact if I just do a little of the work for the compiler (enable USE_FIX), then I no longer get an error. Unfortunately, the "fix" doesn't work generically, only for a specific instantiation of the template.

Is this supposed to work as I expected? Or is this simply not allowed?

BTW: if it matters I am using gcc 4.5.2.

4
  • 1
    It's simply not allowed. If I knew more about your issue I could suggest an appropriate redesign. Jan 13, 2011 at 7:43
  • possible duplicate of How to deduce class type from method type in C++ templates? Jan 13, 2011 at 8:30
  • 4
    The problem with this issue: it's been asked loads of times, but you just cannot find any answer without using the "nondeduced" keyword. But then if you know the keyword, you probably know the answer... searching is difficult :[ Jan 13, 2011 at 8:32
  • 1
    @Matthieu: but hopefully we eventually build up enough duplicates that the searches will find them!
    – Cascabel
    Jan 15, 2011 at 6:11

3 Answers 3

15

The problem with const typename A<T>::U &x is that U is a dependent type and the compiler cannot deduce T from the argument (this is one of the nondeduced context).

You could, for example, have two specializations of A:

class X { };
class Y { };
class Z { };

template <> class A<X> {
public: 
    typedef Z U;
};

template <> class A<Y> {
public:
    typedef Z U;
};

If you then call:

Z a;
a == 1;

what should the compiler deduce T as? X? Y?

One solution in this particular case is to declare operator== as a nontemplate friend inside of the class template:

template <class T = int>
class A {
public:
    class U {
    };

    friend bool operator==(const U& x, int y) {
        return true;
    }

public:
    U f() const { return U(); }
};
10
  • @James : by the way, Z should be declared inside the class template, only then his situation and your example would be fit together. What do you say? I mean, here Z is independent of A, whereas U in the question is a dependent-type.
    – Nawaz
    Jan 13, 2011 at 7:59
  • 3
    @Nawaz: It doesn't matter whether Z is dependent; it matters that U is dependent. Z can be anything. (I had originally used int, but that was wrong because the function template wouldn't even be considered during overload resolution due to the built-in operator, so I introduced Z as a type with no nontemplate overload of operator==). Jan 13, 2011 at 8:08
  • @James : what I meant was that this line Z a; should rather be written as typename A<X>::U a; or typename A<Y>::U a; to pinpoint the similarity of your examples with his situation!
    – Nawaz
    Jan 13, 2011 at 8:18
  • @Nawaz: While it might make it slightly simpler to follow to a casual reader, the fact is that they are 100% equivalent and the compiler does not care at all. After typename A<X>::U a;, typename A<Y>::U and Z are aliases to the same exact type, which is exactly the reason why the type cannot be deduced. Jan 13, 2011 at 8:50
  • 1
    @David : I understand that typename A<X>::U a; and Z a; are exactly same, and that is the reason, as you said, why the type T in operator== cannot be deduced. But these two syntax makes difference to the programmer (not to the compiler), as the synax typename A<X>::U a; makes programmer believe that the compiler can deduce the type T from a for operator==. I believe that is why the topic starter thought it could be a possible bug in the compiler itself, not in his code!! That is all I wanted to say. :-)
    – Nawaz
    Jan 13, 2011 at 9:07
11
template <class T>
bool operator==(const typename A<T>::U &x, int y) {
    return true;
}

Using this template, it is not permissible (or sometimes possible) to deduce the template parameter T from the type of x. It is what is known as a non-deducible context. (E.g. Somebody could specialize A for a different parameter, say double and make A<double>::U a typedef for A<int>::U.)

There is no workaround, you would have to explicitly specify the template parameter which for operator== makes for ugly syntax.

1
  • 1
    I think this explanation is better than James's explanation. Concise and precise. +1
    – Nawaz
    Jan 13, 2011 at 8:02
4

It is not allowed for rather obvious reasons. In general case there's really no way the compiler can deduce the template argument from your call to operator ==. Apparently you assumed that the nested type U uniquely defines the enclosing A specialization. That is not true, which can be illustrated by the following example with two explicit specializations of A

template <> class A<int> {
public:
  class U {};
};

template <> class A<double> {
public:
  typedef A<int>::U U;
};

In this case, if you call templated operator == with an argument of type A<int>::U the compiler cannot deduce template argument T for templated operator ==. Should T be int or double? There's no way to say.

In order to avoid these ambiguities such situations are called non-deduced contexts. Deducing the enclosing class template arguments from a nested type is an example of non-deduced context.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.