P.S. There are known drawbacks/side-effects, though. Related to overlap between this feature and initializer lists. If a class provides normal and initializer list constructors, the latter wins, so:
std::vector<int> values{10}; // creates a vector of 1 element (with value of 10).
--
--
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.
This will not work as it may be expected, as the order in which individual callsof CalculateSomethingWithSideEffect are made is not known; compiler is free toevaluate expressions passed as function arguments in any order.However, it is not free to do so for uniform initialization, the standardrequires strict left-to-right evaluation here.(12.6.1 [class.explicit.init] ¶ 2 and 8.5.4 [dcl.init.list] ¶ 4)Therefore, as a trick, we can translate Bar functioninto a constructor and call it with an initializer list syntax:
--
--
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.
--
I have a use case that I think hasn't been mentioned in this thread yet. https://codereview.chromium.org/1172853004/diff/1/content/browser/bluetooth/bluetooth_dispatcher_host.cc#newcode119 defines a very simple struct:struct BluetoothDispatcherHost::DiscoverySessionOptions {std::vector<BluetoothScanFilter> filters;std::vector<BluetoothUUID> optional_services;};and creates an instance with "new DiscoverySessionOptions{filters, optional_services}". If I had to avoid the initializer list, I'd need to either write a constructor for DiscoverySessionOptions or create a local variable and assign the fields individually.The guideline I'd suggest is that it's valid to use "AggregateType{arguments}" to create an object inline, if AggregateType has no (user-defined) constructors and is defined nearby enough that it's easy to validate that fact. I guess the open question is whether "AggregateType{arguments}" looks different enough from "std::vector{arguments}" to be a viable style rule.I haven't double-checked that my use works in MSVC. If it doesn't, that'd be a good reason to keep it banned.Thoughts?
I have a use case that I think hasn't been mentioned in this thread yet. https://codereview.chromium.org/1172853004/diff/1/content/browser/bluetooth/bluetooth_dispatcher_host.cc#newcode119 defines a very simple struct:struct BluetoothDispatcherHost::DiscoverySessionOptions {std::vector<BluetoothScanFilter> filters;std::vector<BluetoothUUID> optional_services;};and creates an instance with "new DiscoverySessionOptions{filters, optional_services}". If I had to avoid the initializer list, I'd need to either write a constructor for DiscoverySessionOptions or create a local variable and assign the fields individually.The guideline I'd suggest is that it's valid to use "AggregateType{arguments}" to create an object inline, if AggregateType has no (user-defined) constructors and is defined nearby enough that it's easy to validate that fact. I guess the open question is whether
Jeffrey
--
For what it's worth, when we are ready to support this (i.e. when MSVC 2015 has permanently stuck), I think we should follow the Google internal guidance, which is:- style guide allows choice (https://google.github.io/styleguide/cppguide.html#Variable_and_Array_Initialization)- recommend following go/totw:88 (Googlers-only, sorry) guidance, which is roughly:
(and initializer lists [4])
Yes, it seems to me we should probably just move both of them to the allowed section at the same time (with Jeremy's guidelines posted on C++ dos and don'ts).