Question

I am stuck again...

I have this template class:

template <typename a,typename b,typename c>
class myclass
{...};

I would like to declare an object of this class:

myclass<a,b,c> x;

This obviously does not work and I don't seem to be able to work out how to declare this object or if it even is possible.

I can make an object like this:

myclass<int,double,int> x;

But I don't want to specify the types of a,b,c.

Was it helpful?

Solution 2

You need to declare it in template function or class, if I understand you correctly.

template <typename a,typename b,typename c>
void F(){
   myclass<a,b,c> x;
   ...
}

Otherwise it is forbidden by C++ and logic.

OTHER TIPS

The typical way to make complicated template instances without spelling out the arguments is to have a type-deducing function template:

template <typename A, typename B, typename C>
myclass<A, B, C> make_myclass(A a, B b, C C)
{
    return myclass<A, B, C>();   // or whatever
}

Usage:

auto x = make_myclass(1, 'x', -2.5f);   // x is a myclass<int, char, float>

Typical examples from the standard library that use this idiom are make_shared, make_unique, make_pair, make_tuple, make_exception_ptr and make_optional.

there is no typeindependent object in c++, every object needs to be typed when they are declared. The only way to achieve type-erasure is by using technique like void* and pointer conversion, which you can find from boost type erasure

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top