473,394 Members | 1,765 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,394 software developers and data experts.

Heterogeneous containers with CRTP

Hi all,

I've got a question related to emulating aspects of polymorphism
with CRTP. Below is a typical polymorphic class hierarchy with
a definition of a "somewhat" heterogeneous container of objects.

class poly_base
{
public: virtual int foo(int i, int j) = 0;
};

class poly_a : public poly_base
{
public: int foo(int i, int j) { return i + j; }
};

class poly_b : public poly_base
{
public: int foo(int i, int j) { return i * j; }
};

int boo(poly_base* p, int i, int j)
{
return p->foo(i,j);
}

int main()
{
poly_a a;
poly_b b;
poly_base* pa = &a;
poly_base* pb = &b;

std::vector<poly_base*plist;
plist.push_back(pa);
plist.push_back(pb);

int v = 0;
for(std::vector<poly_base*>::iterator it = plist.begin();
it != plist.end();
++it)
{
v += boo((*it),10,20);
}
return 0;
}

I was wondering how would one go about defining a container
of crtp_base's similar to the definition of the vector above
using the below structures.

Is it even possible without using an interface that is abstract?

template <typename T>
class crtp_base
{
public: int foo(int i, int j) { return static_cast<T*>(this)-
>foo(i,j); }
};

class crtp_a : public crtp_base<crtp_a>
{
public: int foo(int i, int j) { return i + j; }
};

class crtp_b : public crtp_base<crtp_b>
{
public: int foo(int i, int j) { return i * j; }
};

template<typename T>
int boo(crtp_base<T>* p, int i, int j)
{
return p->foo(i,j);
}

int main()
{
crtp_a a;
crtp_b b;
crtp_base<crtp_a>* pa = &a;
crtp_base<crtp_b>* pb = &b;
boo(pa,10,20);
boo(pb,10,20);
return 0;
}

Any help would be much appreciated.
Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net

May 14 '07 #1
2 4584
On May 14, 7:10 pm, Arash Partow <par...@gmail.comwrote:
Hi all,

I've got a question related to emulating aspects of polymorphism
with CRTP. Below is a typical polymorphic class hierarchy with
a definition of a "somewhat" heterogeneous container of objects.

class poly_base
{
public: virtual int foo(int i, int j) = 0;

};

class poly_a : public poly_base
{
public: int foo(int i, int j) { return i + j; }

};

class poly_b : public poly_base
{
public: int foo(int i, int j) { return i * j; }

};

int boo(poly_base* p, int i, int j)
{
return p->foo(i,j);

}

int main()
{
poly_a a;
poly_b b;
poly_base* pa = &a;
poly_base* pb = &b;

std::vector<poly_base*plist;
plist.push_back(pa);
plist.push_back(pb);

int v = 0;
for(std::vector<poly_base*>::iterator it = plist.begin();
it != plist.end();
++it)
{
v += boo((*it),10,20);
}
return 0;

}

I was wondering how would one go about defining a container
of crtp_base's similar to the definition of the vector above
using the below structures.
You can't since anything derived from crtp_base<crtp_ais_not_a
crtp_base<crtp_b>.
>
Is it even possible without using an interface that is abstract?

template <typename T>
class crtp_base
{
public: int foo(int i, int j) { return static_cast<T*>(this)-
foo(i,j); }
};

class crtp_a : public crtp_base<crtp_a>
{
public: int foo(int i, int j) { return i + j; }

};

class crtp_b : public crtp_base<crtp_b>
{
public: int foo(int i, int j) { return i * j; }

};

template<typename T>
int boo(crtp_base<T>* p, int i, int j)
{
return p->foo(i,j);

}

int main()
{
crtp_a a;
crtp_b b;
crtp_base<crtp_a>* pa = &a;
crtp_base<crtp_b>* pb = &b;
boo(pa,10,20);
boo(pb,10,20);
return 0;

}

Any help would be much appreciated.

Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.http://www.partow.net

May 15 '07 #2
On May 14, 7:10 pm, Arash Partow <par...@gmail.comwrote:
Hi all,

I've got a question related to emulating aspects of polymorphism
with CRTP. Below is a typical polymorphic class hierarchy with
a definition of a "somewhat" heterogeneous container of objects.

class poly_base
{
public: virtual int foo(int i, int j) = 0;

};

class poly_a : public poly_base
{
public: int foo(int i, int j) { return i + j; }

};

class poly_b : public poly_base
{
public: int foo(int i, int j) { return i * j; }

};

int boo(poly_base* p, int i, int j)
{
return p->foo(i,j);

}

int main()
{
poly_a a;
poly_b b;
poly_base* pa = &a;
poly_base* pb = &b;

std::vector<poly_base*plist;
plist.push_back(pa);
plist.push_back(pb);

int v = 0;
for(std::vector<poly_base*>::iterator it = plist.begin();
it != plist.end();
++it)
{
v += boo((*it),10,20);
}
return 0;

}

I was wondering how would one go about defining a container
of crtp_base's similar to the definition of the vector above
using the below structures.

Is it even possible without using an interface that is abstract?

template <typename T>
class crtp_base
{
public: int foo(int i, int j) { return static_cast<T*>(this)-
foo(i,j); }
};

class crtp_a : public crtp_base<crtp_a>
{
public: int foo(int i, int j) { return i + j; }

};

class crtp_b : public crtp_base<crtp_b>
{
public: int foo(int i, int j) { return i * j; }

};

template<typename T>
int boo(crtp_base<T>* p, int i, int j)
{
return p->foo(i,j);

}

int main()
{
crtp_a a;
crtp_b b;
crtp_base<crtp_a>* pa = &a;
crtp_base<crtp_b>* pb = &b;
boo(pa,10,20);
boo(pb,10,20);
return 0;

}

Any help would be much appreciated.

Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.http://www.partow.net
Either you need to have a common base class, or you need to have types
that have a common interface.

See following example for Heterogeneous Containers that can store
objects that have different base types, but have some type of common
interface (function signature).

http://code.axter.com/HeterogeneousContainer1.cpp
http://code.axter.com/HeterogeneousContainer2.cpp
http://code.axter.com/HeterogeneousContainer3.cpp

May 15 '07 #3

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

Similar topics

0
by: Shawn Spiars | last post by:
dbWidget is a heterogeneous database development tool. It works with most relational databases, including Oracle, SQL Server, MySQL, and Sybase. dbWidget comes with a free, fully functional 30-day...
1
by: kj | last post by:
The main rationale I have seen for namespace support in XML is to enable the peaceful coexistence of XML fragments from various sources within the same ("heterogeneous") XML document without their...
8
by: Markus Dehmann | last post by:
I defined a base class in order to put heterogeneous values into a standard container: All values that I store in the container are derived from my base class. Now when I iterate over the...
7
by: Mike Smith | last post by:
Sorry about the multiple post, but I just realized my prior post was in a thread that dates back a couple of days, so people might not see it. Which is considered better for writing interfaces in...
2
by: Adnan | last post by:
Hi Guys, I have one stored procedure on SQL 6.5 and retrieving data from SQL 2000. I have defined a linked server on SQL 6.5. But I am getting following error:- "Heterogeneous queries require...
2
by: alexander.stippler | last post by:
Hi I try to combine two things, which perhaps aren't compatible at all. But I at least look for alternatives: I want to instantiate objects where the type is chosen based on some input...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
3
by: Frank Bergemann | last post by:
Hi, the (gcc-3.4.4) compiler complains, if i try to use a typedef of subclass in superclass: |padsol15 141make Helper_test Helper_test.cc In file included from Helper_test.cc:17:...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
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
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?
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
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,...
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
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...
0
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...

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.