Skip to content

Instantly share code, notes, and snippets.

@xueliu
Created February 1, 2019 09:51
Show Gist options
  • Save xueliu/8ce6b605329d302c865c64274ba008bc to your computer and use it in GitHub Desktop.
Save xueliu/8ce6b605329d302c865c64274ba008bc to your computer and use it in GitHub Desktop.
make_shared (and make_unique) for classes with private constructors
class Test
{
private:
struct _constructor_tag { explicit _constructor_tag() = default; };
public:
Test(_constructor_tag) {}
static unique_ptr<Test> factory()
{
return make_unique<Test>(_constructor_tag{});
}
};
void test()
{
auto t1 = Test::factory(); // GOOD
auto t2 = make_unique<Test>(); // ERROR
auto t3 = make_unique<Test>()(Test::_constructor_tag); // ERROR
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment