C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template func in class question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Active8
Guest





PostPosted: Sun Feb 27, 2005 7:34 am    Post subject: template func in class question Reply with quote



I have an STL vector I pass to a function and I need to know what
type data is in the vector when I'm in the fuction.

class MyClass
{
template class<T>
int Myfunc(T&){int i = sizeof(T);// oops - I want sizeof(float) }
};

vector<float> vec;
MyClass mc;
mc.Myfunc(vec)

Is there any way to do this without writing separate functions for
each type of data the vector might contain? And without a 2
parameter template *class* like "template class<V, T>" ?

Thanks in advance.
--
Best Regards,
Mike
Back to top
Jonathan Turkanis
Guest





PostPosted: Sun Feb 27, 2005 7:50 am    Post subject: Re: template func in class question Reply with quote



Active8 wrote:
Quote:
I have an STL vector I pass to a function and I need to know what
type data is in the vector when I'm in the fuction.

class MyClass
{
template class<T
int Myfunc(T&)

{
typedef typename T::value_type type;
// Works if you know T will always be an STL container
}
Quote:
};

vector MyClass mc;
mc.Myfunc(vec)

Jonathan



Back to top
Active8
Guest





PostPosted: Sun Feb 27, 2005 8:44 am    Post subject: Re: template func in class question Reply with quote



On Sun, 27 Feb 2005 00:50:00 -0700, Jonathan Turkanis wrote:

Quote:
Active8 wrote:
I have an STL vector I pass to a function and I need to know what
type data is in the vector when I'm in the fuction.

class MyClass
{
template class int Myfunc(T&)

{
typedef typename T::value_type type;
^^^^^^^


I don't understand the use of "typename" above.

I would've said:

typedef T::value_type type;

Quote:
// Works if you know T will always be an STL container
}
};

I wish I could find my Osbourne C++ ref.
--
Best Regards,
Mike

Back to top
Rolf Magnus
Guest





PostPosted: Sun Feb 27, 2005 10:28 am    Post subject: Re: template func in class question Reply with quote

Active8 wrote:

Quote:
On Sun, 27 Feb 2005 00:50:00 -0700, Jonathan Turkanis wrote:

Active8 wrote:
I have an STL vector I pass to a function and I need to know what
type data is in the vector when I'm in the fuction.

class MyClass
{
template class int Myfunc(T&)

{
typedef typename T::value_type type;
^^^^^^^

I don't understand the use of "typename" above.

It tells the compiler that T::value_type is the name of a type. In C++, it's
sometimes hard for the compiler to find out whether a name that depends on
a template is actually a type or not. Therefore, the compiler assumes it's
a value unless you use the typename keyword.

Quote:
I would've said:

typedef T::value_type type;

That would've been wrong, and the compiler should bail out with an error.



Back to top
Active8
Guest





PostPosted: Sun Feb 27, 2005 5:02 pm    Post subject: Re: template func in class question Reply with quote

On Sun, 27 Feb 2005 11:28:40 +0100, Rolf Magnus wrote:

Quote:
Active8 wrote:

On Sun, 27 Feb 2005 00:50:00 -0700, Jonathan Turkanis wrote:

Active8 wrote:
I have an STL vector I pass to a function and I need to know what
type data is in the vector when I'm in the fuction.

class MyClass
{
template class int Myfunc(T&)

{
typedef typename T::value_type type;
^^^^^^^

I don't understand the use of "typename" above.

It tells the compiler that T::value_type is the name of a type. In C++, it's
sometimes hard for the compiler to find out whether a name that depends on
a template is actually a type or not. Therefore, the compiler assumes it's
a value unless you use the typename keyword.

I would've said:

typedef T::value_type type;

That would've been wrong, and the compiler should bail out with an error.

It doesn't (VC++ 6.0) and it works, but thanks the same. That makes
sense. Maybe if I turn the warnings back on...

--
Best Regards,
Mike

Back to top
Jonathan Turkanis
Guest





PostPosted: Sun Feb 27, 2005 5:49 pm    Post subject: Re: template func in class question Reply with quote

Active8 wrote:
Quote:
Rolf Magnus wrote:
Active8 wrote:

I would've said:

typedef T::value_type type;

That would've been wrong, and the compiler should bail out with an
error.

It doesn't (VC++ 6.0) and it works, but thanks the same. That makes
sense. Maybe if I turn the warnings back on...

There's little connection between what a compiler should do and what VC++ 6.0
does. ;-)

Jonathan



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.