[PATCH] Update the coding standards to provide some guidance for a few constructs in C++11

Matt matdzb at gmail.com
Tue Mar 4 02:25:24 PST 2014



================
Comment at: docs/CodingStandards.rst:667
@@ +666,3 @@
+(without any type for temporaries) when doing aggregate initialization or
+something notionally equivalent. Examples:
+
----------------
Chandler Carruth wrote:
> James Dennett wrote:
> > Does use of a constructor taking a std::initializer_list<T> parameter count as "notionally equivalent" to aggregate initialization, or is it under the general "Do not use these...if you care that you're calling some *particular* constructor" case?  I could make an argument for either.
> > 
> > Maybe the easiest way to document that is with an example indicating whether
> > std::vector<int> Very SmallPrimes = { 2, 3, 5 };
> > is permitted or not.
> > 
> > (While I do have a preferred answer, I'm not here to lobby for it, just to ask what the intent is.  Lobbying can happen later.  Much later.)
> Hmm. I'm a bit torn on this one.
> 
> I am OK allowing the variable syntax you give, but I would like to require that if you are creating a temporary, you do something like:
> 
>   std::vector<int>({1, 2})
> 
> I would also be OK with requiring the same syntax for variable declarations:
> 
>   std::vector<int> x({1, 2});
Hi!

Out of curiosity, I'm wondering about the upsides/downsides of following the Scott Meyers' advice in this context.
// "Develop the habit of using brace initialization without “=“.
In: "Overview of the New C++ (C++11)"; http://www.aristeia.com/C++11.html

In "Uniform Initialization Syntax" he provides an example where the alternative would fail:
```
const float * pData = new const float[3] = { 1.f, 2.f, 3.f }; // error
const float * pData = new const float[3] { 1.f, 2.f, 3.f }; // ok
```

Similarly, another example shows that `T var = expr` syntax can’t call explicit constructors; adopting from the example (slightly simplifying, changing `Widget` to `struct`) therein:

```struct Widget
{
	explicit Widget(int);
};
Widget w1(10); // okay, direct init: explicit ctor callable
Widget w2{10}; // ditto
Widget w3 = 10; // error! copy init: explicit ctor not callable
Widget w4 = {10}; // ditto```

Or, is disallowing the above the actual intent?



http://llvm-reviews.chandlerc.com/D2905



More information about the llvm-commits mailing list