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

Started by
5 comments, last by Promit 20 years, 6 months ago
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]
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Advertisement
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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;};


(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...
i thought typedef only applied to data types(ie. int, float, double, char, struct, class) . not keywords (ex. delete, new, if, else, etc).

Beginner in Game Development?  Read here. And read here.

 

delete is an operator, not a type.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
thankyou guys

This topic is closed to new replies.

Advertisement