473,473 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Interface constraints using templates

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.

--
IR
Oct 25 '06 #1
2 1379
IR wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 25 '06 #2
IR
Victor Bazarov wrote:
IR wrote:
>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.
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);
};
};
--
IR
Oct 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
12
by: craigkenisston | last post by:
Hi, I will use an example to do a simplified description of my problem, please don't laugh. I just "believe" I would have to use either interface or abstract classes, but I've not ever write an...
1
by: Kaworu | last post by:
Hi, at first time I want to apologize for my English. I need to change the language of my app and I like to do it changin the caption of the controls in the resource.rc file, or having on...
4
by: Ian Lazarus | last post by:
Why are constraints needed? In C++, an attempt to use a non-existing method will cause a compiler error. Isn't that true in C# also?
1
by: /M | last post by:
Hi! As you know, one way to seperate platform dependent code from platform _independent_ code is to create an abstract base class which acts as an interface towards all platform independent code...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
5
by: Random | last post by:
How can I use reflection (or some other method) to find the type of an object that has been passed in to my method under an interface definition? I try to use GetType, but that won't work.
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.