Skip to content

Instantly share code, notes, and snippets.

@valmat
Created February 16, 2018 17:19
Show Gist options
  • Save valmat/69c67f94daf75452e2ae987aa67d55be to your computer and use it in GitHub Desktop.
Save valmat/69c67f94daf75452e2ae987aa67d55be to your computer and use it in GitHub Desktop.
Solving prblem with emplace_back and private constructor
#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