template constructor in non-template class

  • Thread starter Alexander Stippler
  • Start date
A

Alexander Stippler

Hello,

I want to write a non-template class with a constructor template member.
The constructor shall not take arguments. Is this possible at all?
Here the concrete example: A class which contains three vectors. There
are several different methods to initialize these vectors(size and
contents). These methods are the template parameter I want to use.

enum Method {A, B, C, D, E, F};

class Butcher
{
public:
template <Method m>
Butcher();
private:
Vector a, b, c;
};

template <>
Butcher::Butcher<A>()
: a(2), b(4), c(2)
{
a = 0.5, 1;
b = -1, 1, 3, 2;
c = 0, 1;
}

....

I do not want to call a template function from a normal constructor because
I would have to allocate the vectors twice. Is there a way. I think the
trouble is that constructors are unnamed and I have no argument, from which
the template parameter value can be derived.

regards,
alex
 
T

Tom Widmer

Hello,

I want to write a non-template class with a constructor template member.
The constructor shall not take arguments. Is this possible at all?

No. A workaround is to add a dummy argument of the type

template <Method m>
class MethodWrapper{};

or similar to the constructor.

Tom
 
T

Tom Widmer

No. A workaround is to add a dummy argument of the type

template <Method m>
class MethodWrapper{};

or similar to the constructor.

Or you could create a templated factory function instead of templating
the constructor.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top