Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Two templates with overlap.

Reply
Thread Tools

Two templates with overlap.

 
 
Jason Heyes
Guest
Posts: n/a
 
      12-11-2003
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.


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-11-2003
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:perator>> blah".


 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      12-11-2003

"Jason Heyes" <(E-Mail Removed)> wrote in message
news:3fd88945$0$1019$(E-Mail Removed) ...
> 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


 
Reply With Quote
 
Jason Heyes
Guest
Posts: n/a
 
      12-12-2003
"Chris Theis" <(E-Mail Removed)> wrote in message
news:braae8$e8o$(E-Mail Removed)...
> 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:stream &operator<<(std: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:stream &operator<<(std:stream &os, const SharedPtr<T> &ptr)
{ return os << *ptr; }


Thanks.


 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      12-12-2003

"Jason Heyes" <(E-Mail Removed)> wrote in message
news:3fd90fa4$0$1012$(E-Mail Removed) ...
> "Chris Theis" <(E-Mail Removed)> wrote in message
> news:braae8$e8o$(E-Mail Removed)...
> > 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


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      12-12-2003
On Fri, 12 Dec 2003 11:45:22 +1100, "Jason Heyes"
<(E-Mail Removed)> wrote:

>"Chris Theis" <(E-Mail Removed)> wrote in message
>news:braae8$e8o$(E-Mail Removed)...
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments