lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] () or {} in initializer lists [Was: Default values for default


From: Vadim Zeitlin
Subject: Re: [lmi] () or {} in initializer lists [Was: Default values for default arguments]
Date: Wed, 22 Feb 2017 19:16:33 +0100

On Wed, 22 Feb 2017 17:30:13 +0000 Greg Chicares <address@hidden> wrote:

GC> I want to do this:
GC> 
GC>  InputSequence::InputSequence(std::vector<double> const& v)
GC>      :years_to_maturity_(v.size())
GC> +    ,number_result_    (v)
GC> 
GC> I was ready to commit that, but then I thought I should add a comment:
GC> 
GC> +/// Initializes number_result_ and sets intervals_; keyword_result_ is
GC> +/// only default-initialized.

 I am not sure if it's really worth to have this comment. If anything, I'd
write a comment explaining that some of these vectors are initialized in
the ctor while the others remain empty near their declaration in the
private part of the class.

GC> Yet it is always silly to say in a comment what we can say in code, so
GC> instead of the comment I wrote:
GC> 
GC>  InputSequence::InputSequence(std::vector<double> const& v)
GC>      :years_to_maturity_(v.size())
GC> +    ,number_result_    (v)
GC> +    ,keyword_result_   {}

 This just looks very strange to me. I'd only expect to see such default
initialization for primitive types, i.e. if keyword_result_ were an "int".
But even then I'd use "{0}" for clarity.

GC> but the inconsistent use of () vs. {} looked ugly, so I tried this
GC> (bear in mind that years_to_maturity_ is an int, whereas the other
GC> two are vectors):
GC> 
GC>  InputSequence::InputSequence(std::vector<double> const& v)
GC> -    :years_to_maturity_(v.size())
GC> +    :years_to_maturity_{v.size()}
GC> +    ,number_result_    {v}
GC> +    ,keyword_result_   {}
GC> 
GC> which of course warns of a narrowing conversion on size(),

 I know it's annoying, but is hiding the implicit conversion really better?
As changing years_to_maturity_ to be size_t seems to be a bad idea (it
would probably have cascading effects on other variables...), I'd write
"{static_cast<int>(v.size())}".

GC> so I chose
GC> 
GC>  InputSequence::InputSequence(std::vector<double> const& v)
GC>      :years_to_maturity_(v.size())
GC> +    ,number_result_    {v}
GC> +    ,keyword_result_   {}
GC> 
GC> as the least bad option IMO. But let me know if you disagree.

 Sorry, I don't like it. It's not a huge problem, of course, but this is
both strange looking ("{}") and inconsistent. I'd prefer using static_cast
explicitly if we (still) want to use "{}" for initialization.

 Regards,
VZ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]