# Please confirm my C++ suspicion

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

## Recommended Posts

I want a Window class, for an OS gui library. It would require different members based on the OS. I would preferably not like to have a whole slew of #ifdefs in there. What I'm doing right now is defining many different classes (eg, WindowWin32, WindowLinux, WindowOSX, etc), #including only the one that's wanted, and typedefing it to Window. Now, this works fine, but I'd really like a way of saying "you must have these following public methods", but doing so without inheritance (as it is unecessary, being a compile-time thing). I am reasonably sure even specialised-template classes have no way of garuanteeing this. Confirm for me, so I can get on with coding? Thanks. :)

##### Share on other sites
Instanciation of a function template (or of a method of a class template) will fail if it tries to use members of its argument that do not exist.

If you want to make sure that any templates which you have not used yet will be able to be instantiated (because no members are missing), then I could suggest to create a function template which would verify the existence of all such objects. For instance (untested):

template<typename T> void isWindow() {  // T has a method void Update()  void (T::*update) (void) = &T::Update;  // T defines a type data_type and a function data_type GetData()  typedef typename T::data_type data_type;  data_type (T::*getData) (void) = &T::GetData;}typedef WindowWin32 Window;int main() {  isWindow<Window>();}// Alternative:BOOST_STATIC_ASSERT(& isWindow<Window>);

##### Share on other sites
Inheritance is what you're looking for, so I don't get why you don't want to use it. What's wrong with a 'compile-time thing'? And what do you mean by that anyways, a fair amount of the magic of inheritance is done at run-time (like how you can have a pointer to a base class, but have it really pointing to a derived class)?

##### Share on other sites
Quote:
 Original post by EzbezWhat's wrong with a 'compile-time thing'? And what do you mean by that anyways, a fair amount of the magic of inheritance is done at run-time (like how you can have a pointer to a base class, but have it really pointing to a derived class)?

Actually, he wants to avoid the unnecessary overhead of dynamic dispatch, which he does not need (static dispatch is sufficient in his case).

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by EzbezWhat's wrong with a 'compile-time thing'? And what do you mean by that anyways, a fair amount of the magic of inheritance is done at run-time (like how you can have a pointer to a base class, but have it really pointing to a derived class)?

Actually, he wants to avoid the unnecessary overhead of dynamic dispatch, which he does not need (static dispatch is sufficient in his case).

Okay, I think I misread his post because now that I've reread it, I can't even tell what the OP is asking. :-/

##### Share on other sites
Quote:
 Original post by EzbezOkay, I think I misread his post because now that I've reread it, I can't even tell what the OP is asking. :-/

As I understand it, he wants the user to provide a class that conceptually implements a given interface. However, he does not want to use dynamic dispatch (the interface implementation is used for compile-time polymorphism only).

Also, he would like to make sure that the class does implement the interface without even actually using the class in his code (that is, to be warned as early as possible that the class is incorrect).

##### Share on other sites
Sounds like you should take a look at boost::concept_check.

##### Share on other sites
ToohVyk has it spot on.

Spoonbender - I gave it a cursory glance, I'll have to find reading other than Boost's own to do so. For the moment, however, I have solved it via so:

template <typename T, T F> void CheckFunction(){}template <typename T> class WindowConcept{public:	WindowConcept()	{		CheckFunction< void (T::*)(bool), &T::setVisible >();		CheckFunction< bool (T::*)(void), &T::isVisible >();	}};class F : WindowConcept<F>{public:	void setVisible(bool b)	{	}		bool isVisible()	{		return true;	}};

Which should compile with no errors. However, if you change the return types, the parameter types (or number of), or the method name, there'll be a compile error. Of course, you'll have to create an instantiation of F first.

If anyone knows if there's a way to have the inputs to CheckFunction able to take a function type (aka, void (T*, bool) for setVisible), I'd be greatly appreciative.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 23
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631768
• Total Posts
3002242
×

## Important Information

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!