[Top][All Lists]

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

Re: [lmi] Dynamic in-class initialization immoral?

From: Vadim Zeitlin
Subject: Re: [lmi] Dynamic in-class initialization immoral?
Date: Mon, 6 Mar 2017 03:21:20 +0100

On Mon, 6 Mar 2017 00:51:54 +0000 Greg Chicares <address@hidden> wrote:

GC> On 2017-03-06 00:04, Greg Chicares wrote:
GC> > [///] However, I still have twenty-one files to go,
GC> > which are the thorniest
GC> Is the following patch immoral?


GC> diff --git a/global_settings.hpp b/global_settings.hpp
GC> index ecc8771..20c5465 100644
GC> --- a/global_settings.hpp
GC> +++ b/global_settings.hpp
GC> @@ -27,6 +27,7 @@
GC>  #include "calendar_date.hpp"
GC>  #include "so_attributes.hpp"
GC> +#include <boost/filesystem/operations.hpp> // fs::system_complete()
GC>  #include <boost/filesystem/path.hpp>
GC>  #include <string>
GC> @@ -96,7 +97,8 @@ class LMI_SO global_settings final
GC>      std::string pyx_;
GC>      bool custom_io_0_;
GC>      bool regression_testing_;
GC> -    fs::path data_directory_;
GC> +//  fs::path data_directory_ = (fs::system_complete("."));
GC> +    fs::path data_directory_ {fs::system_complete(".")};
GC>      calendar_date prospicience_date_;
GC>  };

 I think the doubts about morality stem from the superficial similarity
between the above syntax, especially when using "=", and the syntax used
for global variables initialization. However this hides the fact that a
global variable is initialized only once, while a class member is
initialized every time an object of this class is constructed unless a
different value is specified for this member in the ctor member initializer
list (which also covers the case of the default compiler-generated copy

 I.e. to understand how member initializers work, you may apply the
following code transformation: for every declaration "T member{init};", add
a matching "member{init}" to the initializer list of all ctors (creating
the initializer list if necessary) which don't already initialize the given
member (if they do, the member is only initialized once, using the value
from the ctor initializer list and completely ignoring the default
initialization value specified in its declaration).

 But, even simpler, I think to remember this behaviour you only need to
remember that it works in the only reasonable way. Nothing else would
really make sense, would it?

GC> OTOH, can it really be moral? And even if it is, doesn't it still smell
GC> risky?

 No, there is nothing risky about it.


reply via email to

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