473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Two templates with overlap.

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
5 1181
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

"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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dag | last post by:
Hi! I would like to do an overlap window, over my main window (of my application), with a Progress Bar. Exactly when I push a button of my application I want show a window, with a Progress bar,...
2
by: John Baker | last post by:
HI; I have a table where the user is entering data which shows scheduling for member of teams. One problem we have encountered is that sometimes the inputter (is this a word) puts in dates for...
0
by: JIM.H. | last post by:
Hello, I have a ReportViewer and a DropDownList controls in my application. In the design view I see they do not overlap but when I run the application they overlap each other, how can I get them...
1
by: Andrew Poulos | last post by:
I'm using the following to dynamically add a style sheet with larger font sizes: if (document.createStyleSheet) { document.createStyleSheet("extras/styles_large.css"); } else { var oStyle =...
6
by: Robin Haswell | last post by:
Hey guys I was wondering if you could give me a hand with something. If I have two tuples that define a range, eg: (10, 20), (15, 30), I need to determine whether the ranges overlap each other....
6
by: ralphJake | last post by:
hi :} i have been looking for a script like this for a few weeks now, and i am finally giving up and asking for help. everything i found involved preventing overlap in dreamweaver, i want to...
0
by: ltlpeepl | last post by:
I'm having the issue where I have floating spans that overlap in IE6 when wrapping should occur. If you make the browser window small enough so that wrapping will occur, they will overlap, making...
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
5
by: liketofindoutwhy | last post by:
It seems like there are only 4 methods to overlap 2 images using CSS? There are two images, each with its own URL. Using CSS, there seems to be 2 ways to overlap them (the task is actually to put...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.