I want to have a qualified default initialization of a class, even if the constructor has no parameters.
Therefore I am trying to set up a default value with the definition of a template by trying something like that below. Initialize a parameter at compile time (if not given anyways):
template < class Param_t, Param_t def >
class cParameter
{
public:
cParameter( Param_t p = def)
: m_Value(p)
{}
Param_t m_Value;
};
This does not work :-( Addendum: ... with doubles or floats. It works well with ints.
The reason is that I want to replace structure elements in legacy code with as few changes as possible.
typedef struct
{
int someVariable;
float someOtherVariable;
} myStructure_t;
To this
typedef struct
{
cParameter<int> someVariable;
cParameter<float> someOtherVariable;
} myStructure_t;
When this struct is initialized in an ordinary class
class myClass
{
public:
myClass()
: m_Struct()
{}
myStructure_t m_Struct;
}
I get an error (MSVC2013) when the template does not have the default initialization:
template < class Param_t >
class cParameter
{
public:
cParameter( Param_t p )
: m_Value(p)
{}
Param_t m_Value;
};
Error 42 error C2512: 'myClass::m_Struct' : no appropriate default constructor available demo.cpp
I have yet seen the following:
cParameter< int, 1> m_Works;
cParameter< double, 1.0> m_DontWork;
==> The above is now clear to me. I just found the answer here stackoverflow.com/questions/2183087/… (The construct is allowed, but NOT WITH DATATYPE float or double).
But how do I simply get the templates initialized with different values of ints and doubles ?
typedef struct
{
cParameter<int, 42> someVariable;
cParameter<float, 23.0f> someOtherVariable;
} myStructure_t;
What to do here? Any hints?
typedef struct { cParameter