Sign in to follow this  
_goat

Please confirm my C++ suspicion

Recommended Posts

_goat    804
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 this post


Link to post
Share on other sites
ToohrVyk    1595
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 this post


Link to post
Share on other sites
Ezbez    1164
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 this post


Link to post
Share on other sites
ToohrVyk    1595
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).

Share this post


Link to post
Share on other sites
Ezbez    1164
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. :-/

Share this post


Link to post
Share on other sites
ToohrVyk    1595
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).

Share this post


Link to post
Share on other sites
_goat    804
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this