473,414 Members | 1,746 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,414 software developers and data experts.

Template behaves differently with same types

Does anyone know if this code is legal?

template<typename T>
struct holder{

holder(T &t):t_(t){};

T t_;

};

int main(){
int *a = new int[10];
int *front_of_a = &a[0];

holder<int*> h1(a);
holder<int*> h2(front_of_a);

//below does not compile with gcc 3.3.1, icc 6.0, icc 7.0
holder<int*> h3(&a[0]);

}//main

It seems to me like the line where h2 is instantiated should have the
same type as the line with h3 is. But gcc and icc disagree. Does
anyone know what is happening here? The gcc error indicates that T =
int* for h3.

Thanks,
Dave
Jul 22 '05 #1
1 959
On 1 Jul 2004 13:04:55 -0700, David Richardson <dg***@drexel.edu> wrote:
Does anyone know if this code is legal?

template<typename T>
struct holder{
holder(T &t):t_(t){};
T t_;
};

int main(){
int *a = new int[10];
int *front_of_a = &a[0];
holder<int*> h1(a);
holder<int*> h2(front_of_a);

//below does not compile with gcc 3.3.1, icc 6.0, icc 7.0
holder<int*> h3(&a[0]);

}//main

It seems to me like the line where h2 is instantiated should have the
same type as the line with h3 is. But gcc and icc disagree. Does
anyone know what is happening here? The gcc error indicates that T =
int* for h3.

Thanks,
Dave


Nothing to do with templates but it's not legal. The constructor for
holder is a non-const reference and therefore cannot be bound to the
expression &a[0].

holder(T const& t):t_(t){}

would be legal.
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Phillips | last post by:
Hello everyone, I have read the section on templates in The C++ Programming Language 3rd edition 3 times over the last 4 days. Still I am stumped. I'll explain a bit about what I am doing...
3
by: Faith | last post by:
Hi all, I have a serious problem that I am not sure whether its a bug in the Microsoft Visual C++ or something that I am doing wrong. The problem is (as noted in the Subject) is that my program...
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
7
by: PengYu.UT | last post by:
Hi, Suppose I have the following code. I want A::show() behaves differently for template argument with or without std::complex. But it doesn't work. Do you know how to do that? Best wishes,...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
1
by: Vladimir Nesterovsky | last post by:
Hello, I have a code which behaves differently if template parameter has a specified member: #include <iostream> template<class T, bool (T::*)()> class member_bind {
2
by: Amadeus W. M. | last post by:
Template member functions cannot be virtual - I know - but is there any way to simulate that (other than making the entire class templated, with non-templated members). What I have is this: ...
2
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
0
by: James Kanze | last post by:
On May 16, 9:34 am, Paavo Helde <nob...@ebi.eewrote:
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.