Reputation: 41
Can I assign template type say T a type, depending on another template type say U something like this inside a function.
typename <class T, class U>
func(U var)
{
...
if(U == int) //this can be replaced with std::is_same<U, int>
{
T = flaot; //instead int/float i have my own data types
}
...
}
Is this possible.. if so how is it done
Upvotes: 1
Views: 1273
Reputation: 25536
Not sure if it is what you want, but you might try this:
template <typename T>
class Selector; // implement this one as well, if you want to have a default...
template <> class Selector<int> { public: using Type = ClassX; };
template <> class Selector<double> { public: using Type = ClassY; };
template<typename U>
void func(U var)
{
using T = typename Selector<U>::Type;
};
Variant:
template<typename U, typename T = typename Selector<U>::Type>
void func(U var) { }
This would allow to replace the selected type, if need be:
func(12); // uses default: ClassX
func<int, ClassZ>(10); // replace ClassX with ClassZ
Upvotes: 4