c++14

Why to use std::move despite the parameter is an r-value reference

拟墨画扇 提交于 2021-02-07 06:22:22
问题 I am confused about using std::move() in below code: If I uncomment line at (2) the output would be: 1 2 3 but if I uncomment line at (1) output would be nothing which means that move constructor of std::vector was called! Why do we have to make another call to std::move at (1) to make move constructor of std::vector to be called? What I understood that std::move get the r-value of its parameter so, why we have to get the r-value of r-value at (1)? I think this line _v = rv; at (2) is more

Copy elision when creating object inside emplace()

故事扮演 提交于 2021-02-06 15:16:05
问题 I see a lot of code at work where people use emplace and emplace_back with a temporary object, like this: struct A { A::A(int, int); }; vector<A> v; vector<A>.emplace_back(A(1, 2)); I know that the whole point of emplace_back is to be able to pass the parameters directly, like this: v.emplace_back(1, 2); But unfortunately this is not clear to a few people. But let's not dwell on that.... My question is: is the compiler able to optimize this and skip the create and copy? Or should I really try

Copy elision when creating object inside emplace()

旧时模样 提交于 2021-02-06 15:15:37
问题 I see a lot of code at work where people use emplace and emplace_back with a temporary object, like this: struct A { A::A(int, int); }; vector<A> v; vector<A>.emplace_back(A(1, 2)); I know that the whole point of emplace_back is to be able to pass the parameters directly, like this: v.emplace_back(1, 2); But unfortunately this is not clear to a few people. But let's not dwell on that.... My question is: is the compiler able to optimize this and skip the create and copy? Or should I really try

Copy elision when creating object inside emplace()

会有一股神秘感。 提交于 2021-02-06 15:14:44
问题 I see a lot of code at work where people use emplace and emplace_back with a temporary object, like this: struct A { A::A(int, int); }; vector<A> v; vector<A>.emplace_back(A(1, 2)); I know that the whole point of emplace_back is to be able to pass the parameters directly, like this: v.emplace_back(1, 2); But unfortunately this is not clear to a few people. But let's not dwell on that.... My question is: is the compiler able to optimize this and skip the create and copy? Or should I really try

Copy elision when creating object inside emplace()

China☆狼群 提交于 2021-02-06 15:14:30
问题 I see a lot of code at work where people use emplace and emplace_back with a temporary object, like this: struct A { A::A(int, int); }; vector<A> v; vector<A>.emplace_back(A(1, 2)); I know that the whole point of emplace_back is to be able to pass the parameters directly, like this: v.emplace_back(1, 2); But unfortunately this is not clear to a few people. But let's not dwell on that.... My question is: is the compiler able to optimize this and skip the create and copy? Or should I really try

__cplusplus < 201402L return true in gcc even when I specified -std=c++14

有些话、适合烂在心里 提交于 2021-02-06 06:36:13
问题 The directive: #ifndef __cplusplus #error C++ is required #elif __cplusplus < 201402L #error C++14 is required #endif The command-line: g++ -Wall -Wextra -std=c++14 -c -o header.o header.hpp My g++ version: g++ (tdm-1) 4.9.2 The error C++14 is required is generated even when I added -std=c++14 , I don't know why. Please tell me how to fix this. 回答1: According to the GCC CPP manual (version 4.9.2 and 5.1.0): __cplusplus This macro is defined when the C++ compiler is in use. You can use _

__cplusplus < 201402L return true in gcc even when I specified -std=c++14

巧了我就是萌 提交于 2021-02-06 06:35:36
问题 The directive: #ifndef __cplusplus #error C++ is required #elif __cplusplus < 201402L #error C++14 is required #endif The command-line: g++ -Wall -Wextra -std=c++14 -c -o header.o header.hpp My g++ version: g++ (tdm-1) 4.9.2 The error C++14 is required is generated even when I added -std=c++14 , I don't know why. Please tell me how to fix this. 回答1: According to the GCC CPP manual (version 4.9.2 and 5.1.0): __cplusplus This macro is defined when the C++ compiler is in use. You can use _

__cplusplus < 201402L return true in gcc even when I specified -std=c++14

亡梦爱人 提交于 2021-02-06 06:30:50
问题 The directive: #ifndef __cplusplus #error C++ is required #elif __cplusplus < 201402L #error C++14 is required #endif The command-line: g++ -Wall -Wextra -std=c++14 -c -o header.o header.hpp My g++ version: g++ (tdm-1) 4.9.2 The error C++14 is required is generated even when I added -std=c++14 , I don't know why. Please tell me how to fix this. 回答1: According to the GCC CPP manual (version 4.9.2 and 5.1.0): __cplusplus This macro is defined when the C++ compiler is in use. You can use _

Non-ownership copies of std::unique_ptr

╄→尐↘猪︶ㄣ 提交于 2021-02-05 08:58:06
问题 There's two containers: owner and non-owner of resource. As I have only 1 owner, I suppose I need unique_ptr. class OwnershipContainer { public: void add(std::unique_ptr<Obj> obj) { objects.push_back(std::move(obj)); } Obj* get(std::size_t i) { return objects[i].get(); } private: std::vector<std::unique_ptr<Obj>> objects; }; What of kind of pointer i have to use for non-owner container? First thought was raw pointer. But i cannot give guarantee that lifetime of Obj match or exceed lifetime of

Clang 3.7.0 complains of class not being literal because it is not an aggregate and has no constexpr constructors

孤街醉人 提交于 2021-02-05 07:23:20
问题 The following code compiles fine in GCC (4.9.3) and VC++ (19.00.23506) but gives these error in Clang (3.7.0). error: constexpr function's return type 'Foo' is not a literal type note: 'Foo' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors Code: #include <iostream> #include <vector> struct Foo { std::vector<int> m_vec; Foo(const int *foo, std::size_t size=0):m_vec(foo, foo+size) {;} //Foo(const std::initializer_list<int>