Re: Initializer lists vs. 0 or 1 items
Am 27.08.2011 04:24, schrieb Daryle Walker:
[..]
Well, it's a complex-number type, so its implementation would be like
"T c_[2]", so the default construction code would work just fine.
I haven't read it this way, because why in heaven should you then use an
initializer list constructor here? Why not using a normal two-argument
constructor with default arguments as in:
template <typename T>
class my_complex
{
public:
my_complex() = default;
my_complex(T first, T second = T());
};
If you prefer to have the defaulted default constructor, otherwise use:
template <typename T>
class my_complex
{
public:
my_complex(T first = T(), T second = T());
};
But see below for another alternative via delegating constructors.
Provided constructor arguments would fill in elements of the array
until the array or list runs out.
Hmm, there's no way to limit the maximum (or minimum) length of an
initializer list statically, is there?
Not with initializer lists, but why not using a normal, fixed parameter
list instead?
Separately, I've heard about sibling constructor calls. If I want to
minimize duplicated code, I can have my single-argument constructor
reuse the list one:
template< typename T>
class my_complex
{
public:
my_complex( std::initializer_list<T> list = {} );
my_complex( T first )
: my_complex( {first} )
{}
//...
};
Right?
The proper description is a delegating constructor. Yes, this works and
you can apply the very same idiom to a fixed parameter list constructor
as well, like so:
template <typename T>
class my_complex
{
public:
my_complex() = default;
my_complex(T first, T second);
my_complex(T first) : my_complex(first, T()) {}
};
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]