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>:enseVector(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
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>:enseVector(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