By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Two templates with overlap.

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jason Heyes wrote:
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::operator>> blah".
Jul 22 '05 #2

P: n/a

"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f**********************@news.optusnet.com.au ...
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
Jul 22 '05 #3

P: n/a
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:br**********@sunnews.cern.ch...
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::ostream &operator<<(std::ostream &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::ostream &operator<<(std::ostream &os, const SharedPtr<T> &ptr)
{ return os << *ptr; }
Thanks.
Jul 22 '05 #4

P: n/a

"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f**********************@news.optusnet.com.au ...
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:br**********@sunnews.cern.ch...
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?

[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
Jul 22 '05 #5

P: n/a
On Fri, 12 Dec 2003 11:45:22 +1100, "Jason Heyes"
<ge******@optusnet.com.au> wrote:
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:br**********@sunnews.cern.ch...
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?


Yes, it's fine, as long as your compiler is up to date (and therefore
supports partial ordering of function templates).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.