lmi
[Top][All Lists]
Advanced

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

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


From: Greg Chicares
Subject: [lmi] () or {} in initializer lists [Was: Default values for default arguments]
Date: Wed, 22 Feb 2017 17:30:13 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0

On 2017-02-11 16:59, Vadim Zeitlin wrote:
> On Sat, 11 Feb 2017 10:44:00 +0000 Greg Chicares <address@hidden> wrote:
> 
> GC> On 2017-02-11 10:16, Greg Chicares wrote:
> GC> > On 2017-02-10 16:58, Vadim Zeitlin wrote:
[...]
> GC>      :value_number     (0.0)   // parentheses
> GC>      ,value_keyword    {}      // braces?
> GC>      ,value_is_keyword (false) // parentheses
> GC> 
> GC> so should we instead change all the parentheses to braces?
> 
>  Yes, I think we should. But, of course, first we should remove all the
> unnecessary initializer list elements (i.e. those that can be replaced with
> just member initialization as shown above), so there might be not that many
> parentheses to change.
[...]
>  I don't think we want to write initializers for the members which are
> already initialized anyhow, which includes both the members having an
> initializer in their declaration and member objects with default ctor.

It was easy when we had no choice. Now, for reasons that will become clear
with my next git-push, I want to do this:

 InputSequence::InputSequence(std::vector<double> const& v)
     :years_to_maturity_(v.size())
+    ,number_result_    (v)

I was ready to commit that, but then I thought I should add a comment:

+/// Initializes number_result_ and sets intervals_; keyword_result_ is
+/// only default-initialized.

because keyword_result_ used to be (elsewhere) initialized to the same
length as its congener number_result_, but will now be simply be an empty
vector--and this is the only publicly-visible (through an accessor) data
member that's default initialized, so it seems worth emphasizing. Yet it
is always silly to say in a comment what we can say in code, so instead
of the comment I wrote:

 InputSequence::InputSequence(std::vector<double> const& v)
     :years_to_maturity_(v.size())
+    ,number_result_    (v)
+    ,keyword_result_   {}

but the inconsistent use of () vs. {} looked ugly, so I tried this
(bear in mind that years_to_maturity_ is an int, whereas the other
two are vectors):

 InputSequence::InputSequence(std::vector<double> const& v)
-    :years_to_maturity_(v.size())
+    :years_to_maturity_{v.size()}
+    ,number_result_    {v}
+    ,keyword_result_   {}

which of course warns of a narrowing conversion on size(), so I chose

 InputSequence::InputSequence(std::vector<double> const& v)
     :years_to_maturity_(v.size())
+    ,number_result_    {v}
+    ,keyword_result_   {}

as the least bad option IMO. But let me know if you disagree.




reply via email to

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