Two templates with overlap.

J

Jason Heyes

Consider these two templates:

template <class T>
std::istream &operator>>(std::istream &is, T &object);

template <class T>
std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr);

What will the compiler do when it sees the following code?

Foo foo;
cin >> foo;

I presume that the compiler will instantiate the template std::istream
&operator>>(std::istream &is, T &object) with Foo. How about this code?

SharedPtr<Bar> bar;
cin >> bar;

Now I am not sure of what choice the compiler will make. Will it instantiate
the first function or the second? Thanks.
 
G

Gianni Mariani

Jason said:
Consider these two templates:

template <class T>
std::istream &operator>>(std::istream &is, T &object);

template <class T>
std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr);

What will the compiler do when it sees the following code?

Foo foo;
cin >> foo;

I presume that the compiler will instantiate the template std::istream
&operator>>(std::istream &is, T &object) with Foo. How about this code?

SharedPtr<Bar> bar;
cin >> bar;

Now I am not sure of what choice the compiler will make. Will it instantiate
the first function or the second? Thanks.

It should instantiate the second. If you're using namespaces you may
need to have a "using namesp::eek:perator>> blah".
 
C

Chris Theis

Jason Heyes said:
Consider these two templates:

template <class T>
std::istream &operator>>(std::istream &is, T &object);

template <class T>
std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr);

What will the compiler do when it sees the following code?

Foo foo;
cin >> foo;

I presume that the compiler will instantiate the template std::istream
&operator>>(std::istream &is, T &object) with Foo. How about this code?

SharedPtr<Bar> bar;
cin >> bar;

Now I am not sure of what choice the compiler will make. Will it instantiate
the first function or the second? Thanks.

IMHO it should instantiate the second one. Did your compiler do it
differently?

Chris
 
J

Jason Heyes

Chris Theis said:
IMHO it should instantiate the second one. Did your compiler do it
differently?

I still haven't tested it. Let me ask you. Do you think it is a good idea to
use these two templates?

template <class T>
std::istream &operator>>(std::istream &is, T &object)
{ return object.extract(is); }

template <class T>
std::eek:stream &operator<<(std::eek:stream &os, const T &object)
{ return object.insert(os); }

template <class T>
std::istream &operator>>(std::istream &is, SharedPtr<T> &ptr)
{
if (ptr)
{
ptr.make_unique();
return is >> *ptr;
}

T object;
if (!(is >> object))
return is;
ptr = new T(object);

return is;
}

template <class T>
std::eek:stream &operator<<(std::eek:stream &os, const SharedPtr<T> &ptr)
{ return os << *ptr; }


Thanks.
 
C

Chris Theis

Jason Heyes said:
I still haven't tested it. Let me ask you. Do you think it is a good idea to
use these two templates?
[SNIP]

Whether it's a good idea or not actually depends on your problem and the
context but from a quick glance at the code I'd say it should be okay.

Chris
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top