0

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?

Jesko
  • 85
  • 11
  • TBH, I think adding a constructor (and ergo ditching the C-ism that is`typedef struct {`), is the path of least resistance. – StoryTeller - Unslander Monica Feb 21 '18 at 18:13
  • 1
    Maybe you mean something like `template `; otherwise, if you don't give a default value to the template non-type parameter, how can you write `cParameter someVariable;`? – max66 Feb 21 '18 at 18:16
  • I wanted to write so:
    typedef struct { cParameter someVariable; cParameter someOtherVariable; } myStructure_t;
    – Jesko Feb 21 '18 at 18:21
  • @StoryTeller this is more code changes ... – Jesko Feb 21 '18 at 18:25
  • Hang on. You said VS2013? [It supports non-static data member initializers](https://msdn.microsoft.com/en-us/library/hh567368.aspx). Just write `int someVariable = 0;` and so forth. That *would be* less tedious. – StoryTeller - Unslander Monica Feb 21 '18 at 18:30
  • @StoryTeller There is need to replace the parameters by some class. My question is about the initialization **then**. I was interested to get the first code sample to work ... – Jesko Feb 21 '18 at 18:38

1 Answers1

0

If you are OK to have a fixed default per type you can create a type to define it as a constant and specialise it as needed.

template <typename T> struct MyTypeDefault { static const T value; };
template <typename T> const T MyTypeDefault<T>::value = T();
template <> struct MyTypeDefault<double> { static const double value; };
const double MyTypeDefault<double>::value = 1.0;

template <typename T>
class MyType {
    public:
    MyType() { value = MyTypeDefault<T>::value; }
    private:
        T value;
 };
Archie Yalakki
  • 512
  • 4
  • 12
  • looks a little bit impractical. If we have hundrets of such classes and defaults ... Why does this not work in all cases: template class MyType { public: MyType() { value = v; } private: T value; }; – Jesko Feb 22 '18 at 10:56
  • Update: I just found the answer here https://stackoverflow.com/questions/2183087/why-cant-i-use-float-value-as-a-template-parameter?noredirect=1&lq=1 (The construct is allowed, but NOT WITH DATATYPE float or double). Question is: why? – Jesko Feb 22 '18 at 11:01