template problem

M

Mark P

I'm trying to define a templatized version of operator<< to print out
information about a template class. The code below won't compile.
(error: no match for std::eek:stream& << Outer<int>::Inner&) How do I make
this work?

Thanks for your help,
Mark

#include <iostream>

using std::eek:stream;

template <class T>
class Outer
{
class Inner
{
public:
T value;
};

public:
Inner myInner;
};

template <class T>
ostream& operator<< (ostream& out, const typename Outer<T>::Inner& innerObj)
{
return out << innerObj.value;
}

int main()
{
Outer<int> o;
std::cout << o.myInner << std::endl;
}
 
V

Victor Bazarov

Mark said:
I'm trying to define a templatized version of operator<< to print out
information about a template class. The code below won't compile.

The compiler cannot deduce the template argument (supposedly 'int')
(error: no match for std::eek:stream& << Outer<int>::Inner&) How do I
make this work?

What is it you're trying to achieve here?
#include <iostream>

using std::eek:stream;

template <class T>
class Outer
{
class Inner
{
public:
T value;
};

public:
Inner myInner;
};

template <class T>
ostream& operator<< (ostream& out, const typename Outer<T>::Inner&
innerObj) {
return out << innerObj.value;
}

Why can't you simply write

template<class HasValue>
ostream& operator << (ostream& out, const HasValue& obj) {
return out << obj.value;
}

? Why do you think you need 'T' to be 'int'? You're not making any
use of it.
int main()
{
Outer<int> o;
std::cout << o.myInner << std::endl;
}

V
 
M

Mark P

Victor said:
The compiler cannot deduce the template argument (supposedly 'int')
from Outer<T>::Inner. I don't think the context you provided is one
of the allowed contexts for template argument deduction.

Thanks for this info. I think you must be right because it does compile
when I try the much uglier: operator said:
What is it you're trying to achieve here?

A good question :) The code below is of course not what I'm actually
working on but rather my attempt to distill the problem into the fewest
possible lines of code. Admittedly, I did a pretty awful job of it...

Basically, my class has a container of objects, and each of these
objects has a couple containers of T*. I wanted to iterate through the
outer container and call a templatized operator<< on each of the inner
containers. Within the definition of the templatized operator<<, I had
a few calls like

copy (l.begin(),l.end(),std::eek:stream_iterator<T*>(out," "));

which is why I wanted the template parameter to begin with.

Your comments have convinced me that this is a bad approach. I've
instead given the containing classes print() functions which, for my
purposes, are just as good.

Thanks for your help,
Mark
 
D

Donovan Rebbechi

I'm trying to define a templatized version of operator<< to print out
information about a template class. The code below won't compile.
(error: no match for std::eek:stream& << Outer<int>::Inner&) How do I make
this work?

The problem is that the compiler will not implicitly instantiate that
function for you, so the compiler can't find it because it doesn't exist.
The standard and hence compilers offer very little in the way of deduction
when it comes to implicit instantiation of template functions (I guess
probably because it's a potential black hole).

Cheers,
 
J

James Dennett

Donovan said:
The problem is that the compiler will not implicitly instantiate that
function for you, so the compiler can't find it because it doesn't exist.
The standard and hence compilers offer very little in the way of deduction
when it comes to implicit instantiation of template functions (I guess
probably because it's a potential black hole).

Right; particularly in the presence of specializations, there's no
general way to work out what T is from knowing Outer<T>::Inner.

-- James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top