I recently discovered a problem with using std::tuple<>
for just one element. I created a class for type erasure and retaining N number of reference counted objects. However the reference counted object is not retained if it is the only one in the std::tuple<>
.
Am I doing something wrong?
class token {
public:
template<typename... Types>
token(Types... types) : _self(std::make_shared<const std::tuple<Types...>>(std::make_tuple(std::move(types)...))) {}
// Why do I need this special version of the constructor?
// Uncomment and the code will work!
//template<typename T>
//token(T t) : _self(std::make_shared<const T>(std::move(t))) {}
private:
std::shared_ptr<const void> _self;
};
Example (tested with Xcode 8.0):
token make_token() {
std::shared_ptr<int> shared(new int(), [](int* i) {
// Called immediately if using only tuple constructor!
});
return token(shared);
}
token my_token = make_token(); // std::shared_ptr<> is already gone!