What you are trying to achieve does not make sense, because the template parameter is any arbitrary type, and an arbitrary file does not have a concept of "null", but it does not even have to support a concept of some "default initial value". Nothing can be assumed for this type.
There are some work-around approaches. Let's think.
It may seem that you can internally use the pointer to
T
as your private member
m_preset_value
. Then you could assign this member to the 0 pointer, regardless of the actual type of
T
. However, it would not solve problem, only delay it, as you don't know the constructor for
T
if it is required, so how one would instantiate it?
The simplest real resolution could be something like this:
template <class T> class SmartArray {
protected:
T m_preset_value;
virtual void Initialize() = 0;
public:
void SomeMethod() { m_preset_value = Initialize(); } };
This way, with each instantiation of the template you should also create a derived class and the virtual method should be overridden:
class BooleanSmartArray : public SmartArray<bool> {
protected:
virtual void Initialize() { m_preset_value = 0; }
};
This may seem to be not very convenient. As an alternative, you would need to use something like
generic constraints of CLI (.NET, in particular). This way, you could create some interface class with operations like
Initialize
and more. Then, instead of arbitrary unconstrained type
T
, you could indicate that the template parameter is
contrained to be derived from this interface class.
You can see these suggestions by
Bjarne Stroustrup:
http://www.stroustrup.com/bs_faq2.html#constraints[
^].
Another approach is implemented in boost:
http://www.boost.org/doc/libs/1_36_0/libs/concept_check/concept_check.htm[
^].
See also:
http://en.wikipedia.org/wiki/Boost_%28C%2B%2B_libraries%29[
^],
http://www.boost.org/[
^].
And if you can use C++11, you can think about the use of template
std::initializer_list
:
http://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists[
^]
—SA