list-initialization

Possible to initialize/assign a struct pointer?

自作多情 提交于 2021-01-28 11:53:50
问题 Let's say I have the following struct and two versions to initialize it: #include <stdio.h> typedef struct Car *CarPtr; typedef struct Car { const char* name; unsigned int price; } Car; int main(void) { Car ford = { .name = "Ford F-150", .price = 25000 }; print_struct(&ford); // is this possible to do in a single assignment? CarPtr jeep = { .name = "Jeep", .price = 40000 }; print_struct(jeep); } Is the second version possible to do directly? Or do I need to do something along the lines of:

Difference between S() vs S{}?

a 夏天 提交于 2020-02-26 18:20:36
问题 In the code below, is there any difference between the two assignments to value? In both cases, would value.v be default constructed, and x be initialized to 42? struct S { std::vector<int> v; int x = 42; }; S value; void foo() { value = S(); value = { }; } 回答1: S() and S{} mean the same thing in nearly all cases. But not all cases. If S is not a class type, same thing: value initialization. If S is a class type that is not an aggregate, still mostly means the same thing: value initialization

Difference between S() vs S{}?

社会主义新天地 提交于 2020-02-26 18:17:05
问题 In the code below, is there any difference between the two assignments to value? In both cases, would value.v be default constructed, and x be initialized to 42? struct S { std::vector<int> v; int x = 42; }; S value; void foo() { value = S(); value = { }; } 回答1: S() and S{} mean the same thing in nearly all cases. But not all cases. If S is not a class type, same thing: value initialization. If S is a class type that is not an aggregate, still mostly means the same thing: value initialization

Forms of list initialization

只愿长相守 提交于 2020-02-04 03:22:22
问题 See the following code: std::vector<int> v1{1, 2, 3}; std::vector<int> v2 = {1, 2, 3}; My questions are: Is there a difference between the two? I know the first one must be list initialization, but how about the second? Because there is a assign sign for the second, it makes me think that the compiler will use the std::initializer_list to create a temporary vector first, then it use copy constructor to copy the temp vector to v2 . Is this the fact? 回答1: The two (direct-list-initialization vs

Does the equal sign make a difference in brace initialization? eg. 'T a = {}' vs 'T a{}'

时光怂恿深爱的人放手 提交于 2020-01-19 07:34:46
问题 Here are two ways to initialize a variable in C++11: T a {something}; T a = {something}; I tested these two in all scenarios I could think of and I failed to notice a difference. This answer suggests that there is a subtle difference between the two: For variables I don't pay much attention between the T t = { init }; or T t { init }; styles, I find the difference to be minor and will at worst only result in a helpful compiler message about misusing an explicit constructor. So, is there any

Does the equal sign make a difference in brace initialization? eg. 'T a = {}' vs 'T a{}'

旧时模样 提交于 2020-01-19 07:31:18
问题 Here are two ways to initialize a variable in C++11: T a {something}; T a = {something}; I tested these two in all scenarios I could think of and I failed to notice a difference. This answer suggests that there is a subtle difference between the two: For variables I don't pay much attention between the T t = { init }; or T t { init }; styles, I find the difference to be minor and will at worst only result in a helpful compiler message about misusing an explicit constructor. So, is there any

How does guaranteed copy elision work in list-initialization in C++1z?

与世无争的帅哥 提交于 2020-01-14 07:27:14
问题 There is a paragraph about guaranteed copy elision in c++ draft n4606 [dcl.init] 17.6: If the destination type is a (possibly cv-qualified) class type: If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object. [ Example : T x = T(T(T())); calls the T default constructor to initialize x . — end example ] [...] There is also a Q&A talks

Why would they special-case certain initializer lists instead of treating them all the same?

倖福魔咒の 提交于 2020-01-13 14:12:10
问题 Say I have a variable auto x that I want to initialize to 7 using brace initialization, simple: auto x {7}; Except I learned that x is NOT an integer, but an initialization list itself. Why? Is there a specific reason why the committee would decide that auto should grab the initialization list in the case of a single auto value, or do they expect us to just realize these shouldn't be used together. I cant seem to think of a possible reason i would want an initializer list to be stored into

What does “return {}” statement mean in C++11?

血红的双手。 提交于 2020-01-11 15:06:12
问题 What does the statement return {}; in C++11 indicate, and when to use it instead of (say) return NULL; or return nullptr; 回答1: return {}; indicates "return an object of the function's return type initialized with an empty list-initializer". The exact behaviour depends on the returned object's type. From cppreference.com (because the OP is tagged C++11, I excluded the rules in C++14 and C++17; refer to the link for further details): If the braced-init-list is empty and T is a class type with a