Virtual template design question.

Discussion about everything. New games, 3d math, development tips...
Post Reply
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Virtual template design question.

Post by REDDemon »

Does anyone have any elegant solution for solving a problem?


I have a Templated class. by default I use this class with different types (float, double, integer etc.). Now I wan't to give that class different implementations (one for single core execution and 1 for multithreading).

This is not possible in C++ to have virtual template methods. So some possible solutions are

1) write a different interface for every type and then write 2 implementation for each interface. (this requires of adding new methods for exchanging values between the two classes)

2) just add a branch to very method in the old implementation

Code: Select all

//so:
T getValue()
{
    if(multythreadin)
    {
    //new code
        return Value; //of type T
    }
    //old code
        return Value;
}
I still need to return references/pointers to the class or to the value so this solutions is not possible for me:
http://www.java2s.com/Tutorial/Cpp/0260 ... mplate.htm
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

both ways could do the job I think, you could also use:

Code: Select all

T getValue() 
{ 
    #ifdef MUTLITHREADIN
     
    //new code 
        return Value; //of type T 
    #else 
    //old code 
        return Value;
    #endif
}
which should decrease compilation size? though might not be usable if it has selective multithreading
Working on game: Marrbles (Currently stopped).
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Not sure if it's a good idea in this case - but you can do virtual functions with templates which don't use the virtual function mechanism at all, but instead work by passing the derived class as a template parameter.

It's called using the "Curiously recurring template pattern" (http://en.wikipedia.org/wiki/Curiously_ ... te_pattern)

For example:

Code: Select all

#include <iostream> 

template <typename T>
struct Base
{
	void doFoo()
	{
		static_cast<T*>(this)->foo();
	}
};

struct SingleCore : public Base<SingleCore>
{
	void foo()	
	{
		 std::cout << "single core" << std::endl;
	}
};

struct MultiCore : public Base<MultiCore>
{
	void foo()	
	{
		 std::cout << "multi core" << std::endl;
	}
};

int main()
{
	SingleCore sc;
	MultiCore mc;

	sc.doFoo();
	mc.doFoo();

	return 0;
}
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Question that is still unclear to me is whether you want to choose between the implementations during run-time or at compile-time?! You can also pass parameters to use one or the other, and also via templates. This would give something in between your solution 2) and serengeor's.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Post by REDDemon »

I want to do that at runtime. Thanks for suggestions. This wikipedia page was very interesting. Now i found my way :)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply