Created
February 1, 2019 09:51
-
-
Save xueliu/8ce6b605329d302c865c64274ba008bc to your computer and use it in GitHub Desktop.
make_shared (and make_unique) for classes with private constructors
This file contains hidden or 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
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