473,387 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1606
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>::set;
};

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...@mailinator.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
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
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 { }; ...
3
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...
6
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...
2
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...
5
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 ////////////////////////////////////...
2
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
28
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
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.