std::tuple<> 中的一个 std::shared_ptr<> 不起作用?

std::tuple<> of one std::shared_ptr<> does not work?

我最近发现仅对一个元素使用 std::tuple<> 时会出现问题。我创建了一个 class 用于类型擦除并保留 N 个引用计数对象。但是,如果引用计数对象是 std::tuple<>.

中唯一的对象,则不会保留它

我是不是做错了什么?

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;
};

示例(使用 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!

从我的角度来看,您的代码应该可以正常工作,msvc 和 gcc 似乎同意我的看法 this snippet。来自 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';
}

看到这个demo