aggregate-initialization

Private aggregate initialization

元气小坏坏 提交于 2021-02-20 19:02:13
问题 Is it possible to define as private the aggregate initialization for an aggregate class? I would like that the class can only be aggregate-initialized by its own static private members. Example: struct Size { const unsigned int width; const unsigned int height; static const Size big; static const Size small; private: Size( ) = default; // something to declare the aggregare initialization as private }; const Size Size::big = { 480, 240 }; const Size Size::small = { 210, 170 }; 来源: https:/

Type trait for aggregate initializability in the standard library?

守給你的承諾、 提交于 2021-02-18 22:25:34
问题 The C++ standard library has std::is_constructible<Class, T...> to check if a class can be constructed from the given types as arguments. For example, if I have a class MyClass which has a constructor MyClass(int, char) , then std::is_constructible<MyClass, int, char>::value will be true . Is there a similar standard library type trait that will check that aggregate initialization works, i.e. MyClass{int, char} is well-formed and returns a MyClass ? My use case: I want to write a function

Is it possible to prevent omission of aggregate initialization members?

二次信任 提交于 2020-06-24 07:02:08
问题 I have a struct with many members of the same type, like this struct VariablePointers { VariablePtr active; VariablePtr wasactive; VariablePtr filename; }; The problem is that if I forget to initialize one of the struct members (e.g. wasactive ), like this: VariablePointers{activePtr, filename} The compiler will not complain about it, but I will have one object that is partially initialized. How can I prevent this kind of error? I could add a constructor, but it would duplicate the list of

Is it possible to prevent omission of aggregate initialization members?

混江龙づ霸主 提交于 2020-06-24 07:02:06
问题 I have a struct with many members of the same type, like this struct VariablePointers { VariablePtr active; VariablePtr wasactive; VariablePtr filename; }; The problem is that if I forget to initialize one of the struct members (e.g. wasactive ), like this: VariablePointers{activePtr, filename} The compiler will not complain about it, but I will have one object that is partially initialized. How can I prevent this kind of error? I could add a constructor, but it would duplicate the list of

C++20 initializing aggregates from a parenthesized list of values, not supporting inner array

孤街浪徒 提交于 2020-06-11 11:45:46
问题 C++20 adopted p0960 - allowing initialization of aggregates from a parenthesized list of values. The exact wording ([dcl.init] 17.6.2.2) says: [...] if no constructor is viable, the destination type is an aggregate class, and the initializer is a parenthesized expression-list, the object is initialized as follows. Let e 1 , …, e n be the elements of the aggregate ([dcl.init.aggr]). Let x 1 , …, x k be the elements of the expression-list. If k is greater than n, the program is ill-formed. The

Do empty braces call the default constructor or the constructor taking an std::initializer_list?

前提是你 提交于 2019-12-22 04:32:33
问题 The following is a quote from Effective Modern C++ (page 55): "Suppose that you use an empty set of braces to construct an object that supports default constructor and also supports std::initializer_list construction. What do your empty braces mean? etc. The rule is that you get default construction." I tried this with std::array: std::array<int, 10> arr{}; and got the warning from g++ (version 4.8.2): warning: missing initializer for member ‘std::array<int, 10ul>::_M_elems’ which is the

Why can I initialize a regular array from {}, but not a std::array

不羁的心 提交于 2019-12-22 04:09:38
问题 This works: int arr[10] = {}; All elements of arr are value-initialized to zero. Why doesn't this work: std::array<int, 10> arr({}); I get the following warning from g++ (version 4.8.2): warning: missing initializer for member ‘std::array<int, 10ul>::_M_elems’ 回答1: There are two issues one which is a matter of style and the warning. Although it may not be obvious, aggregate initialization is happening on a temporary which is then being used as an argument to the copy constructor. The more

When is a private constructor not a private constructor?

丶灬走出姿态 提交于 2019-12-17 04:54:27
问题 Let's say I have a type and I want to make its default constructor private. I write the following: class C { C() = default; }; int main() { C c; // error: C::C() is private within this context (g++) // error: calling a private constructor of class 'C' (clang++) // error C2248: 'C::C' cannot access private member declared in class 'C' (MSVC) auto c2 = C(); // error: as above } Great. But then, the constructor turns out to not be as private as I thought it was: class C { C() = default; }; int

When initializing a struct with ={} syntax, what's happening under the hood?

我的未来我决定 提交于 2019-12-12 03:55:41
问题 edit Tweaked example a little based on comments A little code then the question (just to clarify, this is a C++ question): #include <cstdio> struct MYSTRUCT1 { int asdf[4]; } MyStruct1; struct MYSTRUCT2 { int asdf[4]; MYSTRUCT2() : asdf() {} } MyStruct2; template <class T> void test() { T blah = {{1,-1,1,-1}}; for( int ii = 0; ii < 4; ii++ ) { printf( "%d ", blah.asdf[ii] ); } printf( "\n" ); } int main() { // Works fine; MyStruct1 doesn't define a constructor test<MyStruct1>(); // Doesn't

What is the behavior when there are more initializers than array size?

…衆ロ難τιáo~ 提交于 2019-12-10 23:29:45
问题 I would like to know what happens when there are more initializers than array size, e.g. : int t[3] = { 1, 2, 3, 4 }; Of course, my compiler warns it. I expected undefined behavior, but I didn't find any clause about it in C11 standard. So, did I miss something ? 回答1: The code is ill-formed in both C and C++. C++11 §8.5.1[dcl.init.aggr]/6 states: An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize. C11 §6.7.9/2 states: