Interface constraints using templates

I

IR

Hi,

Still playing around with templates, I have the following
questions...


I know how to express "positive" compile time assertions of a
class's interface in templates (ie. T has-a member with this
signature), using a syntax similar to:

template<class T>
struct InterfaceEnforcer
{
InterfaceEnforcer()
{
void (T::*sig1)() const = &T::Function1;
};
};

and inheriting from (or declaring a variable of type)
InterfaceEnforcer<MyClass>


I would like to know if it is possible to:


1) make a "partial" interface check on T, something like:

template<class T, class U>
struct InterfaceEnforcer
{
InterfaceEnforcer()
{
U (T::*sig1)() const = &T::Function1;
};
};

but this very syntax (and others I tried around the same idea)
doesn't work.


2) check that a specific member does NOT exist in T. I don't even
have an idea of how to express this concept. I don't think it is
possible, but would value a confirmation.


Any ideas anyone?
Thanks in advance.
 
V

Victor Bazarov

IR said:
Still playing around with templates, I have the following
questions...


I know how to express "positive" compile time assertions of a
class's interface in templates (ie. T has-a member with this
signature), using a syntax similar to:

template<class T>
struct InterfaceEnforcer
{
InterfaceEnforcer()
{
void (T::*sig1)() const = &T::Function1;
};
};

and inheriting from (or declaring a variable of type)
InterfaceEnforcer<MyClass>


I would like to know if it is possible to:


1) make a "partial" interface check on T, something like:

template<class T, class U>
struct InterfaceEnforcer
{
InterfaceEnforcer()
{
U (T::*sig1)() const = &T::Function1;
};
};

but this very syntax (and others I tried around the same idea)
doesn't work.

Are you looking for something like "has_member" or "is_member_of"?
If you search Google Groups for "has_member", you might just find
what you need...
2) check that a specific member does NOT exist in T. I don't even
have an idea of how to express this concept. I don't think it is
possible, but would value a confirmation.

What you do is a compile-time assertion using SFINAE and then use
the opposite of it, IOW let it fail.

V
 
I

IR

Victor said:
Are you looking for something like "has_member" or "is_member_of"?
If you search Google Groups for "has_member", you might just find
what you need...


What you do is a compile-time assertion using SFINAE and then use
the opposite of it, IOW let it fail.

Thanks for the pointers, I didn't know it was named "has_member"
(nor about that SFINAE thing). Lots of reading ahead...

I now understand _why_ (2) is impossible. :)

As to (1), I found how to implement it:

template<typename T>
class InterfaceEnforcer
{
private:
template<typename U>
void EnforceAnyFoo(U (T::* _foo)())
{
U (T::*Foo)()= _foo;
};
public:
InterfaceEnforcer()
{
EnforceAnyFoo(&T::Foo);
};
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top