Enabling make_unique with Private Constructors
Quote from 'STL':
That's not actually sufficient, since it's possible to have an omniconvertible X with an implicit conversion to Y, returning Y{}.
One way to really lock this down is to give Z a templated ctor, enabled only when the tag is Y.
Quote from 'STL':
That's not actually sufficient, since it's possible to have an omniconvertible X with an implicit conversion to Y, returning Y{}.
One way to really lock this down is to give Z a templated ctor, enabled only when the tag is Y.
class Test
{
struct _constructor_tag { explicit _constructor_tag() = default; }; // As 'STL' mentioned, this should't be just empty default constructor since client can init. with {}.
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
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.