Advertisement Jump to content
Sign in to follow this  

Asking types if they have a function

This topic is 4903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heh, this has been bothering me for a while. Templates are great, and they are compile time objects. Or at least they get 'restructured' at compile time. So why is it not possible to provide handler support where it exists, and fall back to generic routines were it doesn't? Here's an example:
class FileClass
   void write_data(void *data, size_t num_bytes) {...}

   template <class Type>
   void SaveData(Type *data)
      if( Type::Save(FileClass*) ) // Or any kind of conditional statement
         data->Save( this );
         write_data( data, sizeof(Type) );
Is anything like this possible? Since the compiler generates a new instance of SaveData() for each type, there would be no cost in providing conditional generation. It would allow a programmer to write a function which allows specific objects to require themselves handled differently than anything else. And the routines sending these objects to some random task would not even need to worry about such things. Only the template function and the type would be involved. I'm not looking for a new way of doing things. Just curious if something like this already exists. Thanks :)

Share this post

Link to post
Share on other sites
You could use a base class, and specialize the SaveData() member function for that base class. The unspecialized implementation could simply call write_data().

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!