Please confirm my C++ suspicion

Started by
6 comments, last by _goat 17 years, 4 months ago
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. :)
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Advertisement
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>);


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)?
Quote:Original post by Ezbez
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)?


Actually, he wants to avoid the unnecessary overhead of dynamic dispatch, which he does not need (static dispatch is sufficient in his case).
Quote:Original post by ToohrVyk
Quote:Original post by Ezbez
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)?


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. :-/
Quote:Original post by Ezbez
Okay, 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).
Sounds like you should take a look at boost::concept_check.
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]

This topic is closed to new replies.

Advertisement