specialized static template variables

A

Alexander Stippler

Hello,

I have some template class, where I want to initialize an array
with some default value depending on a template parameter. Therefore
I have some traits class. Just like this:

template <typename T>
DenseVector<T>::DenseVector(long size)
{
_allocate(); // allocate memory;
std::uninitialized_fill_n(vector_, size,
AllocatorTraits<T>::defaultValue());
}

The AllocatorTraits class is just like this:

template <typename T>
class AllocatorTraits
{
public:
static const T &
defaultValue();

private:
static T value_;
};

I have to initialize value_ somehow, so I set it to T(). Now my question:
I want to be able to overwrite the value of value_ for different template
parameters (neglect visibility concerns). And this overwriting shall happen
somewhat 'statically', i.e. before any object of DenseVector<T> gets
instatiated. Is there a way other than specializing the whole
AllocatorTraits
class again and again? The only problem is the point of time, at which I
have to
have the value set. In other words by (unrealistic) example:
How can I manage that
AllocatorTraits<DenseVector<double> >::defaultValue() yields 10
whereas
AllocatorTraits<DenseVector<long> >::defaultValue() yields 20
and have these default values BOTH set initially BEFORE instatiating
DenseVector at all and WITHOUT specializing AllocatorTraits.

regards,
alex
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top