Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with functions accepting inner classes of template classes

Tags:

c++

templates

I have a problem with inner classes in class templates. I have a template class (say: Matrix<T>), and a subclass (say: Matrix<T>::Row). Now I want to to write a function which operates on instances of the subclass (say: negate(Matrix<T>::Row &)). I tried to declare the function with template<class T> negate(typename Matrix<T>::Row &), but when I try to use it, the compiler tells me that it cannot find a match.

Here's an abstract example:

template<class T>
class A
{
public:
    class B
    {
    };
};

template<class T>
void x(typename A<T>::B &)
{
}

int main()
{
    A<int>::B b;
    x(b); // doesn't work: Error: Could not find a match
          // for x<T>(A<int>::B) needed in main().
    x<int>(b); // works fine
}

Why does the compiler does not manage to find x in the first case? Is there a way to modify this that it works (without explicitly specifying the type int)?

(I also have similar problems where x is of the form template<class T, class S> void x(typename A<T>::B &, const S &);, whence I would really like not to be forced to explicitly name all types while doing the call.)

I have tried this with g++ 4.4.3, g++ 4.5.2, and Sun Studio 5.9, all give the same result. Thanks a lot in advance for anything helpful!

like image 568
felix Avatar asked Aug 24 '11 16:08

felix


2 Answers

How should the compiler be able to deduce this? Imagine the following setup:

struct A { typedef int T; };
struct B { typedef int T; };

template <typename S> void foo(typename S::T);

Now when you say int x; foo(x);, there's no way to match this unambiguously.

The point is that you are not deducing a template parameter from a given class template, but rather just an arbitrary, free-standing type. The fact that that type was defined inside another class is not relevant for that.

like image 73
Kerrek SB Avatar answered Sep 20 '22 05:09

Kerrek SB


That is non-deducible context. That is why the template argument cannot be deduced by the compiler.

Just imagine, you might have specialized A as follows:

template <>
struct A<SomeType>
{
    typedef std::map <double, double> B;
};

Now this specialization has a nested type called B which is a typedef of std::map<double,double>.

So how would the compiler deduce the type SomeType, given that A<SomeType>::B is std::map<double, double>?

And in fact, there can be many such specializations, as such:

template <>
struct A<SomeOtherType>
{
    typedef std::map <double, double> B;
};

Even this specialization has B as nested type.

Now if I say A<T>::B is std::map<double,double>, then can you say what T is? Is it SomeType? or SomeOtherType?

like image 23
Nawaz Avatar answered Sep 21 '22 05:09

Nawaz