Created
February 16, 2018 17:19
-
-
Save valmat/69c67f94daf75452e2ae987aa67d55be to your computer and use it in GitHub Desktop.
Solving prblem with emplace_back and private constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
class A | |
{ | |
private: | |
struct private_key {}; | |
// private constructor | |
A(int a) : | |
a(a) | |
{ | |
std::cout << "A("<< a <<")" << std::endl; | |
} | |
public: | |
// indirectly (by private_key) private constructor | |
A(int x, private_key&&) : A(x) {} | |
A(A&& rhs) | |
{ | |
std::cout << "A(A&&)" << std::endl; | |
a = rhs.a; | |
rhs.a = 0; | |
} | |
static std::vector<A> make() | |
{ | |
std::vector<A> result; | |
result.emplace_back(1, private_key()); | |
result.emplace_back(2, private_key()); | |
return result; | |
} | |
private: | |
int a; | |
}; | |
int main() | |
{ | |
auto a {A::make()}; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment