--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
I suppose the counterpoint I would make is that I prefer to see what
the constructor is doing that's not obvious (zeroing out all the
primitive fields, creating a WeakPtrFactory, etc., are).
I think that the Google guide strikes a good balance, but I can
appreciate your concerns. I'm not sure what you're proposing, though;
do you want to restrict it to small structs?
I should add, though, that caveat I missed in my initial email was
that this is sadly not supported for bit-fields in C++11.
As a result, here's what the cc::ResourceProvider::Resource looks like
using in-class member initialization.
https://codereview.chromium.org/606733003/
Note that the delegating-constructor equivalent also has a drawback,
in that delegating constructors cannot have any other initializers.
--
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
Hmm, this is a feature I hate in Java and I didn't realize that C++11 allowed it. I find it so confusing to have the initialization of a class distributed over the constructor and this kind of inline initialization. "initialized the same way in more than one constructor" can easily be handled by delegated constructors. Basically, without this feature, you only need to read the constructor to know how the variable is initialized, whereas with the feature, you need to look at declaration and constructors.
I don't understand how delegated constructors are any different in this regard.
With a delegated constructor, you still have to look at both the
initializing constructor (for what it initializes) and the called
constructor (for what it assigns).
On Thu, Sep 25, 2014 at 2:22 PM, Hendrik <hend...@chromium.org> wrote:
> I'm not proposing that we restrict anything, only that I prefer all
> intializations to be in one place. I trust other developers to make
> decisions that make sense, I'd rather allow all C++11 features + a bit of
> education on the pitfalls.
>
> Not being able to initialize bit-fields is annoying. In this case, I'm
> wondering why these are even bit-fields, this class does not get created
> thousands of times, and doesn't need to be space compact.
>
> Ignoring the bit-fields, was there a reason you didn't also initialize all
> of the other variables (e.g. |type| enum?), when I see that change I look at
> the header and wonder how many initialized variables we have. I know this
> doesn't stop us from missing some (I fixed one today), but it's difficult to
> look at two sets of variables and see if the union of the set captures
> initialization for every variable.
tl;dr: I don't feel strongly about this, and the Google style guide
doesn't seem to comment on it.
In the case of the |type| enum, I wasn't convinced there one that was
a natural "zero".
My ultimate preference would be to have the *compiler* validate that
there are no uninitialized members (except where whitelisted),
and
then to specify as defaults only things which make are logically
default/zero. Then the compiler would force you to supply a reasonable
value for each constructor.
But in the absence of that I don't care which approach is taken. A
rule like Alex's SGTM.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
Use in-class member initialization for simple initializations, especially when a member variable must be initialized the same way in more than one constructor.
class Method { public: enum class Kind { kSimple, kNormal, kAsync, kRaw }; Method(const std::string& name_in, const std::vector<Argument>& input_arguments_in, const std::vector<Argument>& output_arguments_in) : name(name_in), input_arguments(input_arguments_in), output_arguments(output_arguments_in) {} Method(const std::string& name_in, const std::vector<Argument>& input_arguments_in) : name(name_in), input_arguments(input_arguments_in) {} explicit Method(const std::string& name_in) : name(name_in) {} std::string name; std::vector<Argument> input_arguments; std::vector<Argument> output_arguments; Kind kind = Kind::kNormal; bool is_const = false; };Here we have a Method class that has a few constructors dealing with initialization of name, in/out arguments of a method. But none of them actually touch properties like kind and is_const since they are rarely used. To me it makes perfect sense to split the members that can be initialized differently via constructors and the rest which can just be set to their defaults right in the class body...
Perhaps with the some guidance, e.g.:* Prefer not to use them when you need to initialize other members in the constructor (i.e. try to keep your initialization together)* Prefer not to use them when the type being initialized is complex
--
We just allowed delegated constructors, right? It seems like those can similarly be used to avoid the problem of creating a new member and only remembering to initialize it in one of multiple constructors, without scattering initialization across class declarations and class definitions. This also sidesteps the question of which initializations are "simple" and which aren't. Allowing this seems like optimizing for the writer of the code and not the reader of the code, especially since we just greenlighted a different mechanism for addressing the same concern.
I think we have enough for this week, so we should at least wait a bit.I think tooling hasn't quite caught up with this language feature yet: If all constructors set a variable that also has a direct initializer, compilers don't seem to diagnose that the in-class initializer is ignored. I think we should implement a warning for this, but that doesn't block using the feature.I checked that a POD with in-class initializers doesn't cause static initializers, so that's good.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
Is any blocking issue here?
On Mon Jan 12 2015 at 8:41:05 PM Ryo Hashimoto <hash...@chromium.org> wrote:
Replying to an old thread.Can we revisit this now?Is there anything preventing us from allowing this to let our style guide align with Google C++ style guide?On Sat, Nov 8, 2014 at 8:30 AM, Nico Weber <tha...@chromium.org> wrote:On Fri, Nov 7, 2014 at 3:13 PM, Nico Weber <tha...@chromium.org> wrote:I think we have enough for this week, so we should at least wait a bit.I think tooling hasn't quite caught up with this language feature yet: If all constructors set a variable that also has a direct initializer, compilers don't seem to diagnose that the in-class initializer is ignored. I think we should implement a warning for this, but that doesn't block using the feature.I checked that a POD with in-class initializers doesn't cause static initializers, so that's good.(a global instance of such a POD, that is.)--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev