Jump to content
  • Advertisement
Sign in to follow this  
Jiia

Asking types if they have a function

This topic is 4841 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
{
public:
   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 );
      else
         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
Advertisement
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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!