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: Greg Chicares
Subject: Re: [lmi] () or {} in initializer lists [Was: Default values for default arguments]
Date: Sun, 26 Feb 2017 18:44:46 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0

On 2017-02-22 18:16, Vadim Zeitlin wrote:
> 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.

I want to revise the documentation of the initialize_from_vector() function
template anyway, and I think that'll be the best place to mention this.

> 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.

Yes, if the horns of this dilemma are silliness and strangeness, then
the latter is not necessarily preferable, and we need to look for a
better resolution. I look at the snippet immediately above and just
wonder "what was the author thinking?".

> 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())}".

A safe cast that throws on actual narrowing would be a better choice
here IMO (I'm planning to work on it, but haven't made the time yet).
I would object to "{static_cast<int>(v.size())}" because it doesn't
resolve the problem that "{}" is trying to tell us about--it merely
tells the compiler to ignore it.

> 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.

Agreed. It's gone in commit ccdffc3657cd5cc9836fb7efeebed51d424a0ac4.




reply via email to

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