Template behaves differently with same types

  • Thread starter David Richardson
  • Start date
D

David Richardson

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
 
J

John Harrison

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.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top