473,788 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with function lookup and inheritance

I need to know if it is possible to solve the following problem (I am
really stuck at it).

Consider the CRTP pattern:

struct Derived
{
....// May contain void operate( double x ) , may not.
};

template<class D>
struct Base : D
{
....// May contain void operate( double x ) , may not.
void set( double x )
{
static_cast<D*> ( this )->operate( x );
}

};

D derived;
derived.set( 1.0 );

What I would like to accomplish, is to have derived.set( 1.0 ) behave
EXACTLY as if the set function were defined inside of Derived.

More precisely, I would like to obtain the same "function lookup"
behaviour for "operate" that I would obtain if set were defined inside
Derived, that is: search for operate inside of Derived, and if not
present search for it inside Base, and if not present gives compile
error.

I know I can search for the existence of "operate" in Derived using
SFINAE to decide if I should do the cast or not.
However, this does not solve my problem because what happens in
reality is that "operate" can be defined in another class from which
Derived inherits.

Basically, I need "something like" static_cast that also searches in
the bases of the converted class, and not only in the particular
converted class. Something like *moving the lookup starting point".

This is the key problem that I need to solve to finish a component of
my library.
Thank you very much in advance for any help.

Best Regards
StephQ
Dec 7 '07 #1
4 1619
Alf P. Steinbach wrote:
* StephQ:
>>
More precisely, I would like to obtain the same "function lookup"
behaviour for "operate" that I would obtain if set were defined
inside Derived, that is: search for operate inside of Derived, and
if not present search for it inside Base, and if not present gives
compile error.

What's wrong with simply calling the function, then?
StephQ doesn't seem to want to call the function if it's defined in
one of the base classes of the 'D' type (inside the 'Base' template).

Of course I may have misunderstood.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 7 '07 #2
I just discovered (I made a slight error in my code example) that the
previous example works fine if "operate" is in Derived or in another
class (not Base) from which Derived inherits.
So it seems that static_cast is actually doing what I am demanding and
the lookup searches for operate in the base classes.

However I still have TWO problem with my example. Consider the
following code:

// mu and sigma are just empty struct in namespace tag for tagging
purpouses.

template<class D>
struct par
{
template<class A1>
void set( const A1& a1 )
{
typedef typename A1::key_type tag1;
static_cast<D*> ( this )->set_obj( a1, tag1() );
}

template<class A>
void set_obj( const A& a, tag::mu )
{
}

template<class A>
void set_obj( const A& a, tag::sigma )
{
}
};

struct muvar
{
typedef tag::mu key_type;
};

struct sigmavar
{
typedef tag::sigma key_type;
};

class Base : public par<Base>
{
public:

using par<Base>::set;
};

class Derived : public par<Derived>, public Base
{
public:

//template<class A>
//void set_obj( const A& a, tag::mu )
//{
//}
//template<class A>
//void set_obj( const A& a, tag::sigma )
//{
//}

using par<Derived>::s et;
};

int main()
{
Derived derived;

derived.set( muvar() );
}

Basically I am using tags to select the implementation. The
implementation inside par is the default one.
However this code does not work in the following situations:

1) set_obj is only in par (as in the code here) =I get ambiguous
error
2) set_obj( const A& a, tag::sigma ) is uncommented in Derived =I
get no overload found error

How can I solve these problems?

Thank you.

StephQ
Dec 7 '07 #3
StephQ doesn't seem to want to call the function if it's defined in
one of the base classes of the 'D' type (inside the 'Base' template).

Of course I may have misunderstood.

V
See my last reply. Basically for a given tag (say tag::mu) I want to
select set_obj( .., tag::mu) searching first in Derived, and then if
not present in all the bases of Derived. Obviously I am perfectly fine
with getting an error if two ore more candidates are present.
But I can not get this working with this implementation.

Thank you for your replies.

Best Regards
StephQ
Dec 7 '07 #4
On Dec 7, 7:54 pm, StephQ <askmeo...@mail inator.comwrote :
StephQ doesn't seem to want to call the function if it's defined in
one of the base classes of the 'D' type (inside the 'Base' template).
Of course I may have misunderstood.
V

See my last reply. Basically for a given tag (say tag::mu) I want to
select set_obj( .., tag::mu) searching first in Derived, and then if
not present in all the bases of Derived. Obviously I am perfectly fine
with getting an error if two ore more candidates are present.
But I can not get this working with this implementation.

Thank you for your replies.

Best Regards
StephQ
Hi,
IMHO, try avoiding MI. Check the following.
Regards,
Francesco

namespace tag
{
struct mu {};
struct sigma {};
}

template< typename TBridge >
struct CBridger : TBridge {};

template< >
struct CBridger< void {};
//template<class D>
template<class D, typename TBridge = void >
//struct par
struct par : CBridger< TBridge >
{
template<class A1>
void set( const A1& a1 )
{
typedef typename A1::key_type tag1;
static_cast<D*> ( this )->set_obj( a1, tag1() );
}

template<class A>
void set_obj( const A& a, tag::mu )
{
}

template<class A>
void set_obj( const A& a, tag::sigma )
{
}

};

struct muvar
{
typedef tag::mu key_type;
};

struct sigmavar
{
typedef tag::sigma key_type;
};

class Base : public par<Base>
{
public:
using par<Base>::set;

};

//class Derived : public par<Derived>, public Base
class Derived : public par<Derived, Base >
{
public:
//template<class A>
//void set_obj( const A& a, tag::mu )
//{
//}
template<class A>
void set_obj( const A& a, tag::sigma )
{
}

using par<Derived, Base>::set;
using par<Derived, Base >::set_obj;

};

int main()
{
Derived derived;
derived.set( muvar() );

}
Dec 9 '07 #5

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

Similar topics

11
19034
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
13
2828
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; template <typename Base> class Tpl : protected Base {
3
1641
by: Steven T. Hatton | last post by:
I stumbled upon this blog while googling for something. I have to say, I really don't understand what Lippman is trying to tell me here. I included the first paragraph for context, but the second paragraph is the one that has me most confused. <quote url="http://blogs.msdn.com/slippman/archive/2004/01/27/63473.aspx"> In C++, a programmer can suppress a virtual call in two ways: directly use an object of the class, in which case the...
6
4410
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
5
1867
by: dragon | last post by:
error! 33 E:\program\wukexin\file\file.h `bool My_lib::Cfileinfo::initialize(const char*)' is private 19 E:\program\wukexin\dir\dir.cpp within this context //////////////////////////////////// #ifndef fileH #define fileH //// #include "windows.h" namespace My_lib{
2
1850
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
28
4336
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
3
2903
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table: tlkpColor (PK) tlkpColorID
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.