473,666 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Abstract base class requires a virtual template: fixes?

Hello -

I have a problem where I need to test some numeric code using a
variety of built-in integer types:

obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct?

If I test with 10 built-in integer types, then I get 1000 permutations
of the above statement. If I then test a dozen different operators, I
get over 10,000 test operations.

Obviously, I need some generic way to handle this. I can't immediately
see a static/template-based solution, but it seems like I should be
able to do this dynamically. My first pass at a solution looks like:

1 - define an 'integer object' abstract base class, IntObject

2 - define a virtual templated method 'get()' in IntObject, which
returns a pointer to an object of a templated integer type

3 - define a derived class for each integer type of interest; this
stores an object of that type, and defines the virtual 'get()'
function to return a pointer to this object

4 - my test code can now declare an array of IntObject pointers, and
can randomly select a pointer in this array. Calling the 'get()'
method then returns an object pointer of an effectively random type
for testing:

IntObject* res_array, opL_array, opR_array;
...
res_index = rand() % 10;
opL_index = rand() % 10;
opR_index = rand() % 10;
*(res_array[res_index]->get()) =
*(opL_array[opL_index]->get()) OP *(opR_array[opR_index]->get());

The problem is, this doesn't work, because C++ doesn't allow templated
virtuals:

class IntObject {
public:
template<typena me T>
virtual T* get(void) const = 0; // ERROR
};

Any ideas on how I can get around this problem, or any better
solutions?

Many thanks -

Dom
Jun 27 '07 #1
2 1891
On 27 Jun, 12:16, Dom Jackson <nos...@mci2000 .comwrote:
Hello -

I have a problem where I need to test some numeric code using a
variety of built-in integer types:

obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct?

If I test with 10 built-in integer types, then I get 1000 permutations
of the above statement. If I then test a dozen different operators, I
get over 10,000 test operations.

Obviously, I need some generic way to handle this. I can't immediately
see a static/template-based solution, but it seems like I should be
able to do this dynamically. My first pass at a solution looks like:

1 - define an 'integer object' abstract base class, IntObject

2 - define a virtual templated method 'get()' in IntObject, which
returns a pointer to an object of a templated integer type

3 - define a derived class for each integer type of interest; this
stores an object of that type, and defines the virtual 'get()'
function to return a pointer to this object

4 - my test code can now declare an array of IntObject pointers, and
can randomly select a pointer in this array. Calling the 'get()'
method then returns an object pointer of an effectively random type
for testing:

IntObject* res_array, opL_array, opR_array;
...
res_index = rand() % 10;
opL_index = rand() % 10;
opR_index = rand() % 10;
*(res_array[res_index]->get()) =
*(opL_array[opL_index]->get()) OP *(opR_array[opR_index]->get());

The problem is, this doesn't work, because C++ doesn't allow templated
virtuals:
also, there is no way for a compiler to deduce T from call to get().
class IntObject {
public:
template<typena me T>
virtual T* get(void) const = 0; // ERROR

};

Any ideas on how I can get around this problem, or any better
solutions?
not as easy as you've tried. look up some sort of variant
like boost::variant

say you have int and long integer types to work with

typedef boost::variant< int, longmy_integer;

provide visitor for each operation and each
type combination (sorry).

struct plus_visitor : public boost::static_v isitor<my_integ er>
{
my_integer operator()(int i, int j) const
{
return my_integer(i+j) ;
}
my_integer operator()(long i, long j) const
{
return my_integer(i+j) ;
}
my_integer operator()(int i, long j) const
{
return my_integer((lon g)i+j);
}

my_integer operator()(long i, int j) const
{
return my_integer(i+(l ong)j);
}
};

it also works for template operators if you can
treat types generically

struct reveal : public boost::static_v isitor<void>
{
template<class T>
void operator()(T const & t) const
{
std::cout << t <<'\n';
}
};

int main()
{
my_integer i(1); // hold int
my_integer j(1L); // holds long

// k = i + j;
my_integer k = ::boost::apply_ visitor(plus_vi sitor(), i, j);

::boost::apply_ visitor(reveal( ), k);
}

That is the best I can think of.
Many thanks -

Dom
regards

DS

Jun 27 '07 #2
Dom Jackson a écrit :
Hello -

I have a problem where I need to test some numeric code using a
variety of built-in integer types:

obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct?

If I test with 10 built-in integer types, then I get 1000 permutations
of the above statement. If I then test a dozen different operators, I
get over 10,000 test operations.

Obviously, I need some generic way to handle this. I can't immediately
see a static/template-based solution, but it seems like I should be
able to do this dynamically. [snip]
Perhaps a typelist is what you need. Giving an example with addition:

//generic typelist system
template <typename T1, typename T2>
struct TypeList
{
typedef T1 Head;
typedef T2 Tail;
};
//end of list type
struct NullType {};

// define a list with your types
typedef TypeList<long, TypeList<int, TypeList<short, NullType IntList;

//implementation of the plus operation as recursive template
template < typename TList1 , typename TList2 , typename TListInistruct
plus_operation_ imp;

//general case of recursion
template < typename T1, typename T2 , typename T3, typename T4 ,
typename TListIni>
struct plus_operation_ imp< TypeList<T1, T2, TypeList<T3, T4,
TListIni :
public plus_operation_ imp< TypeList<T1, T2, T4, TListIni>
{
T1 operator()(T1 lhs,T3 rhs)
{
return lhs+rhs;
}
};

//recursion at end of list for right hand side
template < typename T1, typename T2 , typename T3 , typename TListIni>
struct plus_operation_ imp< TypeList<T1, T2, TypeList<T3, NullType>
,TListIni:
public plus_operation_ imp< T2 , TListIni, TListIni>
{
T1 operator()(T1 lhs,T3 rhs)
{
return lhs+rhs;
}
};
//recursion at end of both list
template < typename T1, typename T3 , typename TListIni>
struct plus_operation_ imp< TypeList<T1, NullType, TypeList<T3,
NullType, TListIni>
{
T1 operator()(T1 lhs,T3 rhs)
{
return lhs+rhs;
}
};

//type to initialize recursion
template<typena me TList>
struct plus_operation: public plus_operation_ imp<TList,TList ,TList>
{};

//here is your functor
plus_operation< IntListmyoperat ion;

Then you can call myoperation with any combinaison of parameters.
myoperation(lon g,long);
myoperation(lon g,int);
myoperation(int ,int);
myoperation(int ,long);
....

You might even automatize that with MPL but I am not familiar enough
with it to tell you.

Michael
Jun 27 '07 #3

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

Similar topics

7
1520
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: // Assignment operators. virtual base<T>& operator=(const base<T>& rhs) = 0;
4
4204
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real implementation usage of these concepts? Where these concepts will be used. Please provide some illustration (real-time), so that it can be easily comprehended. Thanks, wittyGuy
33
3333
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
8
4037
by: Asfand Yar Qazi | last post by:
Hi, I have the following header file in my 'everything useful I think of in one place' library: ============= BEGIN CODE SNIPPET =========== /** All classes that derive from this obtain a 'virtual constructor' - ie if the 'clone()' method is called on a polymorphic type, an object of the same (unknown) type is returned. */
0
2674
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
6
3302
by: Nindi | last post by:
5 Files Singleton.h The Singleton Factory.h The factory creating new objects. The Base class of the hierachy stores a typelist identifying the signature of the constructors to be called in the hierachy by the Factory StaticFactory Returning refernences to a static object .. typelist as above.
4
6565
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this right? And, why not use "new" instead of using "virtual"? And the last question, what is the differences between a abstract method and a interface?
17
3526
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
6
4024
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it would be useful to have /some/ data defined in such an abstract class for reasons of forming a common block of data existing in or used by all descendant classes (see example below.) In such a case where a common block of data *always* exists,...
0
8443
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
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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
7385
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
5663
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();...
1
2769
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.