temporary-objects

Statement that isn't a full-expression

南笙酒味 提交于 2021-02-08 21:01:21
问题 It is often heard that in C++ temporary objects get deconstructed at the end of the full-expression . A full-expression is defined to be an expression that isn't a sub-expression of some other expression. This sounds very similar to the notion of a statement to me. So my questions are: If I append a semi-colon to a full-expression , will it always be a statement ? Can one arrive at every full-expression by taking some statement with a semi-colon at the end, and removing that semi-colon? Can I

Statement that isn't a full-expression

China☆狼群 提交于 2021-02-08 21:01:09
问题 It is often heard that in C++ temporary objects get deconstructed at the end of the full-expression . A full-expression is defined to be an expression that isn't a sub-expression of some other expression. This sounds very similar to the notion of a statement to me. So my questions are: If I append a semi-colon to a full-expression , will it always be a statement ? Can one arrive at every full-expression by taking some statement with a semi-colon at the end, and removing that semi-colon? Can I

C++ temporary class instantiation ambiguously

坚强是说给别人听的谎言 提交于 2021-01-27 10:40:27
问题 Let we have procedure formed as class. Only constructor call makes some side effect. No need to handle class instance in memory after call. Following code instantiate that class: struct A{ A(int){} }; int main() { A(0);//right. Pass const to ctor int x=0; A(x);//bad. Compiler interpret like A x; (A(x));//right. New temporary object passed to brackets A((int)x);//right. Pass temporary int to ctor return 0; } (see also on Online IDE) Why A(x); interpret as variable x declaration instead of

Validity and/or lifetime extension of mem-initializer in aggregate initialization

南笙酒味 提交于 2021-01-20 20:01:27
问题 CWG 1815 asked (with minor edits): struct A {}; struct B { A&& a = A{}; }; B b1; // #1 B b2{A{}}; // #2 B b3{}; // #3 [...] #2 is aggregate initialization, which binds B::a to the temporary in the initializer for b2 and thus extends its lifetime to that of b2 . #3 is aggregate initialization, but it is not clear whether the lifetime of the temporary in the non-static data member initializer for B::a should be lifetime-extended like #2 or not, like #1 . Per the Notes on that issue, at Issaquah

What is unfortunate about the construction given in the following example?

走远了吗. 提交于 2021-01-03 05:48:06
问题 Section "15.6.2 Initializing bases and members" (N4713) has the following example following item 11: struct A { A() = default; // OK A(int v) : v(v) { } // OK const int& v = 42; // OK }; A a1; // error: ill-formed binding of temporary to reference A a2(1); // OK, unfortunately What is unfortunate about the construction in the last line of the example? I searched the whole reference for other occurrences of "unfortunate" behaviour that were permitted but I could find none. If it was

What is unfortunate about the construction given in the following example?

China☆狼群 提交于 2021-01-03 05:47:13
问题 Section "15.6.2 Initializing bases and members" (N4713) has the following example following item 11: struct A { A() = default; // OK A(int v) : v(v) { } // OK const int& v = 42; // OK }; A a1; // error: ill-formed binding of temporary to reference A a2(1); // OK, unfortunately What is unfortunate about the construction in the last line of the example? I searched the whole reference for other occurrences of "unfortunate" behaviour that were permitted but I could find none. If it was

Range-v3's zip function works with temporaries coming from other Range-v3's functions but not with others

浪尽此生 提交于 2020-12-13 03:29:44
问题 (This is kind of a follow up to this other question.) Original question The following code works just fine #include <boost/range/adaptor/transformed.hpp> #include <cmath> #include <range/v3/view/zip.hpp> #include <string> #include <vector> int main() { auto vec1 = std::vector<int>{1,2,3}; auto vec2 = std::vector<std::string>{"uno","due","tre"}; auto sqrt = [](auto x){ return std::sqrt(x); }; auto vec3 = vec1 | boost::adaptors::transformed(sqrt); for (auto const& i : ranges::views::zip(vec1,

wstring::c_str() contains garbage

我的梦境 提交于 2020-01-24 23:45:31
问题 I have a std::wstring decode(const char *s) function. I use it like this: const char *src = "some string"; const wchar_t *result = decode(src).c_str(); I always get garbage in result[0] , sometimes in result[1] too. I dont get garbage when I use it in another way: std::wstring res = decode(src); const wchar_t *result = res.c_str(); My decode function defined as below, and it does it's job. Only problem is calling code(above). std::wstring decode(const char *s, UINT cp=CP_ACP) { if(s == NULL)

Object creation and destruction order in C++

扶醉桌前 提交于 2020-01-16 03:50:40
问题 I wrote a simple program to learn more about the order of creating and destructing objects in C++ (using Visual Studio 2015). Here it is: #include <iostream> #include <string> using namespace std; class A { public: A(string name) : name(name) { cout << "A(" << name << ")::constructor()" << endl; } ~A() { cout << "A(" << name << ")::destructor()" << endl; } private: string name; }; class C { public: C(string name, A a) : name(name), a(a) { cout << "C(" << name << ")::constructor()" << endl; }

Object creation and destruction order in C++

只愿长相守 提交于 2020-01-16 03:50:30
问题 I wrote a simple program to learn more about the order of creating and destructing objects in C++ (using Visual Studio 2015). Here it is: #include <iostream> #include <string> using namespace std; class A { public: A(string name) : name(name) { cout << "A(" << name << ")::constructor()" << endl; } ~A() { cout << "A(" << name << ")::destructor()" << endl; } private: string name; }; class C { public: C(string name, A a) : name(name), a(a) { cout << "C(" << name << ")::constructor()" << endl; }