Std :: tuple <> from one std :: shared_ptr <> not working?

I recently discovered a problem using std::tuple<>for only one item. I created a class to erase the type and saved N the number of counted objects. However, the reference counted object is not preserved if it is the only one in 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!
+4
source share
1 answer

, , msvc gcc, , . T.C. , clang clang trunk

, (special_decay_t cppreference):

#include <iostream>
#include <tuple>
#include <memory>

template <class T>
struct unwrap_refwrapper
{
    using type = T;
};

template <class T>
struct unwrap_refwrapper<std::reference_wrapper<T>>
{
    using type = T&;
};

template <class T>
using special_decay_t = typename unwrap_refwrapper<typename std::decay<T>::type>::type;

class token {
public:
  template<typename... Types>
  token(Types&&... types) : _self(std::make_shared<std::tuple<special_decay_t<Types>...>>(std::forward<Types>(types)...)) {}

private:
  std::shared_ptr<void> _self;
};


token make_token() {
  return token(std::shared_ptr<int>(new int(), [](int* i) {
    std::cout << "freed\n";
    delete i;
  }));
}

int main()
{
    token my_token = make_token();
    std::cout << __LINE__ << '\n';
}

+1

Source: https://habr.com/ru/post/1661781/


All Articles