Archived

This topic is now archived and is closed to further replies.

Promit

(C++) Working with a non-typed template?

Recommended Posts

Promit    13246
Suppose i have a template like so:
template
struct SomeType
{
    int SomeID;
    T* Data;
}
  
And I want to write a routine that works with that struct and SomeID, but doesn't care about Data. Could I write a function that takes a SomeType instead of a SomeType<> and works with it like that? (This is just for curiosity's sake, btw, no real application at the moment) Actually, now that I think of it, could I write a templatized function and just use the template class as the SomeType class type to achieve the same thing? [edited by - Promit on October 9, 2003 9:12:54 PM]

Share this post


Link to post
Share on other sites
Fruny    1658
Could I write a function that takes a SomeType instead of a SomeType<> and works with it like that?

No. Declaring a SomeType template doesn''t mean ''plain'' SomeType exists.

Actually, now that I think of it, could I write a templatized function and just use the template class as the SomeType class type to achieve the same thing?

Yes. Either
template <class T> int Func( T arg );
or
template <class T> int Func( SomeType<T> arg );



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
null_pointer    289
You could also put SomeID into a non-templated base class, e.g.:


struct SomeTypeBase { int SomeID; };

template <class T> struct SomeType : public SomeTypeBase
{
T* Data;
};


Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
(Off topic)


//Just wondering, but why isn''t this typdef legal????:

typedef delete delete_the_thing_pointed_to_by;
int* ip = new int;
delete_the_thing_pointed_to_by ip;


I don''t think anybody knows...

Share this post


Link to post
Share on other sites