Meta-programming, templates, virtual member functions

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Meta-programming, templates, virtual member functions

 
0
  #1
Feb 5th, 2009
Hello all,

Before anything else, this issue is also posted at: http://www.cplusplus.com/forum/general/7518/


I have a structure similar to the one below.I miss a lot of information about the specific template parameters, so I would need something like what is in the code. This code does not compile because we cannot have templatized virtual member functions. Does anyone have a clever idea how to solve this? The result I want is to get through the specialized operator() of B<O,true>.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class SomeClass;
  5.  
  6. /* In the real code I have SomeClass< T, P, Q>
  7.  * and this is not known when the compiler reaches A,B and C*/
  8. struct OtherClass{
  9. int i;
  10. double d;
  11. OtherClass(int ii, double dd):i(ii),d(dd){};
  12. };
  13.  
  14. /* My idea of A is this, although it is not
  15.  * possible to have a virtual template function */
  16. class A{
  17. public:
  18. template<class K>
  19. virtual void operator()(K input )=0;
  20. };
  21.  
  22. template<int L, bool M, class S>
  23. struct B : public A{
  24. template<class K>
  25. void operator()(K input ){ cout << "Inside B" << endl; };
  26. };
  27.  
  28. template<class S>
  29. struct B<0, true, S> : public A{
  30. template<class K>
  31. void operator()(K input ){ cout << "Inside B<0,true>"<< endl; };
  32. };
  33.  
  34. /* This class creates and stores an object of type B (or one of the specializations).
  35.  * But L and M are unknown so I created A to store the B object here
  36.  * K is also unknown here. */
  37. class C{
  38. A* object;
  39. public:
  40. C(){
  41. /*
  42. * Get some data from files and according
  43. * to this data construct a B specialized object. */
  44. object = new B< 0, true, SomeClass>();
  45. };
  46.  
  47. void go(){
  48. /*
  49. * Later, I want to call B, that was stored through the interface A
  50. */
  51. (*object)(new OtherClass(0,2));
  52. }
  53. };
  54.  
  55. int main(void){
  56. C* c = new C();
  57. c->go();
  58.  
  59. return 0;
  60. };
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #2
Feb 5th, 2009
So who do you want to answer it or are you happy wasting the time of two sets of people?

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #3
Feb 5th, 2009
Well, so far, nobody replied. But if I am freaking out someone I can remove it. Why is it a waste of time? And, if you consider it a waste of time what are you doing here? Are you being paid or something?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #4
Feb 5th, 2009
Originally Posted by rmlopes View Post
Well, so far, nobody replied. But if I am freaking out someone I can remove it. Why is it a waste of time? And, if you consider it a waste of time what are you doing here? Are you being paid or something?
This is my point, we are not paid. This means we volunteer out time to help provide you with a solution in which somebody on another site is already discussing in his/her own time. Meaning the same topics get covered multiple times on different forums wasting more peoples time. No doub you will find somone such as Salem is a member of both forums, and he may well refuse to post in either, despite the fact he is perhaps one of the most valuable people to you.

If we were paid we wouldn't care

Chris
Last edited by Freaky_Chris; Feb 5th, 2009 at 12:56 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #5
Feb 5th, 2009
Well, I know you re not being paid. However, that doesn't give you the right to be rude, as I think you were in your first post. You're just one more specialist in something, like there are a lot of them out there. Anyway thank you for your attention. I am pretty sure that people do waht I did, without even warning.
So, I will remove this post from here and if later I still need I will close it on the other site and open it here.

Cheers,
Rui
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #6
Feb 5th, 2009
Sorry if I come across rude. Yes alot of people do it, and they also get a LOT of stick for it thats for sure.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Meta-programming, templates, virtual member functions

 
0
  #7
Feb 7th, 2009
Well first off, I accept that you didn't get any replies at the previous forum, so I will accept that you should re-post. (I might have waited another day or so but that is minor). Given that, I would like to comment on your problem, and actually ask a few quesitons really.

First off: C++ forbids template virtual functions of any sort. You can normally code round that in a slightly ugly way. Several methods around depending on the probelms (many based on encapsulating the function in a template class)

Second: I think that you are trying to do some kind of template dispatch ???? If this is the case then you should look at the boost mpl.

An effective method of doing what I thnk you want is this:

  1. #include <iostream>
  2.  
  3. class A { public: static void call() { std::cout<<"This is A"<<std::endl;} };
  4. class B { public: static void call() { std::cout<<"This is B"<<std::endl;} };
  5.  
  6. class Base { public: virtual void go() {std::cout<<"Base"<<std::endl; } };
  7.  
  8. template<typename T>
  9. class hold : public Base
  10. {
  11. public:
  12.  
  13. hold() : Base() {}
  14. void go() { T::call(); }
  15. };
  16.  
  17.  
  18. class C
  19. {
  20. Base* object;
  21.  
  22. public:
  23.  
  24. C() : object(new hold<A>()) {}
  25.  
  26. template<typename T>
  27. void setHold() { delete object; object=new hold<T>; }
  28. void go() { object->go(); }
  29. };
  30.  
  31. int main()
  32. {
  33. C cobj;
  34. cobj.go();
  35. cobj.setHold<B>();
  36. cobj.go();
  37. return 0;
  38. }

The above code basically, sets an internal state variable object, that is an effective virtual object. That results in one extra level of dereference. [I think it was Andrei Alexandrescu who said that when faced with a problem the solution is to add a level of dereference.] This is such a case. You have moved from method to object, and then it works.

Note that I have not fixed any of the delete operators, so this leaks memory. It does almost what you want, and has runtime flexiblity. If this is not what you are after please post again. BUT this time make your example shorter and the question slightly more explicit.
Last edited by StuXYZ; Feb 7th, 2009 at 9:34 am.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #8
Feb 9th, 2009
Hello, and thank you for the reply. Yes, what I am trying to do is some kind of template dispatch. I could have made the example a bit shorter maybe but not much. I need, in fact, the template parameters at the class B (A or B in your example). I think I have two main problems: first, I only know the classes' template parameters in runtime (thus I cannot store a A<> or B<> object and I need a holder); the second, is that the function that is going to be called takes an argument which type must be deduced, using a template in the function (this template function is the one that would be virtual). I tried to get a way of propagating this argument's type untill the place where class B is instanciated, avoiding this problematic templated function, but I could not do it.

Rui
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Meta-programming, templates, virtual member functions

 
0
  #9
Feb 9th, 2009
I am a bit lost: You cannot create a class from template parameters that are not known at compile time. For example
  1. template<int N>
  2. class A
  3. { };
You can't have A<i> and since you will only have created explicit instances. e.g. A<0> , A<1>. You can do dispatch of a known sequence. e.g 1-10 but that is your lot with templates.

As for adding parameters that are deduced at runtime, that is ok
do that dispatch first , then do the class dispatch (via hold).
In that case put the parameters into hold. OR do class then the dispatch on runtime, boost::any is a quick way to impliment that.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 31
Reputation: rmlopes is an unknown quantity at this point 
Solved Threads: 0
rmlopes rmlopes is offline Offline
Light Poster

Re: Meta-programming, templates, virtual member functions

 
0
  #10
Feb 10th, 2009
You cannot create a class from template parameters that are not known at compile time
Well, I know this That's why I am having such problems. I have one class, say C, that, in the constructor, reads a file and according to this file will aggregate a object, say B, which has an int template parameter and implements the desired behavior for C. This way I can associate the file contents with different specializations of B (B<0>, B<1>, etc). The first problem is that in C I don't know which specialization of B will be stored. The second problem is that the function in B is supposed to get an argument which type is also unknown, therefore needs to be a template function, not allowing the virtualization.

As for adding parameters that are deduced at runtime, that is ok
do that dispatch first , then do the class dispatch (via hold).
In that case put the parameters into hold. OR do class then the dispatch on runtime, boost::any is a quick way to impliment that.
I am not sure I understood this part. In the meanwhile I have a (ugly) solution already, using boost::any you mencioned. I found a way to propagate the argument's type untill the constructor of C where I can build the B specializations passing the argument type, which will then be used in boost::any_cast<ArgType>. The current code model is this:

  1. #include <iostream>
  2. #include "boost/any.hpp"
  3. using namespace std;
  4.  
  5. class SomeClass;
  6. struct OtherClass{
  7. int i;
  8. double d;
  9. OtherClass(int ii, double dd):i(ii),d(dd){};
  10. };
  11.  
  12. struct Base{
  13. virtual double call(boost::any in)=0;
  14. };
  15.  
  16. template<int L, bool M, class S, class T>
  17. struct B : Base{
  18. double call(boost::any in ){ cout << "Inside B" << endl; return 0;};
  19. };
  20.  
  21. template<class S,class T>
  22. struct B<0, true, S,T>:Base{
  23. double call(boost::any in){ cout << "Inside B<0,true> and obj.i is "<< boost::any_cast<T*>(in)->i<< endl; return 0;};
  24. };
  25.  
  26. template<class Q>
  27. class C{
  28. Base* i;
  29. public:
  30. template< class T >
  31. C( T* t ){
  32. i = new B< 0, true, SomeClass, T>();
  33. };
  34.  
  35. template<class T>
  36. void go( T* t ){
  37. i->call( boost::any(t) );
  38. }
  39. };
  40.  
  41. int main(void){
  42. typedef C<SomeClass> CS;
  43. CS* c = new CS(new OtherClass(2,1));//dummy object
  44. c->go(new OtherClass(0,2));
  45.  
  46. return 0;
  47. };

Related to propagation of the argument type: for this I am using a templatized constructor in C and a dummy argument to deduce the type. I have enough information to do this like in a regular function: C<ArgType>(). Is there a way to do something like this or to avoid having to propagate the arg type untill de B objects?

Thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC