lmi
[Top][All Lists]
Advanced

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

Re: [lmi] Default values for default arguments


From: Vadim Zeitlin
Subject: Re: [lmi] Default values for default arguments
Date: Sat, 4 Feb 2017 15:59:10 +0100

On Sat, 4 Feb 2017 04:51:35 +0000 Greg Chicares <address@hidden> wrote:

GC> Vadim--Do you see any reason why we should not prefer the uniform
GC> "{}" syntax to specify default values for default arguments as in
GC> the following examples? E.g., does any compiler you customarily
GC> use not support this yet?
GC> 
GC> 
---------8<--------8<--------8<--------8<--------8<--------8<--------8<-------
GC> diff --git a/input_sequence.hpp b/input_sequence.hpp
GC> index b4dfe6c..dc77158 100644
GC> --- a/input_sequence.hpp
GC> +++ b/input_sequence.hpp
GC> @@ -262,10 +262,9 @@ class LMI_SO InputSequence
GC>          ,int                             a_retirement_age
GC>          ,int                             a_inforce_duration
GC>          ,int                             a_effective_year
GC> -        ,std::vector<std::string> const& a_allowed_keywords
GC> -            = std::vector<std::string>()
GC> +        ,std::vector<std::string> const& a_allowed_keywords = {}
GC>          ,bool                            a_keywords_only = false
GC> -        ,std::string const&              a_default_keyword = ""
GC> +        ,std::string const&              a_default_keyword = {}
GC>          );
GC>  
GC>      InputSequence(std::vector<double> const&);
GC> 
--------->8-------->8-------->8-------->8-------->8-------->8-------->8-------

 The answer to the question above depends on what exactly do you mean by
"this". The code similar to above can be compiled just fine with all of gcc
(4.9, 5, 6), clang (3.8, 4.0) and MSVS 2015. But, just in case it wasn't
obvious not only to me, this relies on the existence of the ctor from
std::initializer_list in both std::string and std::vector. IOW, this
wouldn't work for any class not providing such ctor, so if the question is
"can we uniformly use {} to specify default values for all classes", then
the answer is definitely no.

 Personally I'm a bit wary of everything involving std::initializer_list in
C++, it's too simple to write something that compiles without any warnings
but doesn't behave like you thought it would with it (of course, the worst
offender here is the notorious std::vector<int> ctor, but there are other
examples too). So while it is convenient to use it in these 2 particular
(and, admittedly, very common) cases, I'm not sure it's worth the extra
potential for confusion that it creates.

 Regards,
VZ


reply via email to

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