大佬教程收集整理的这篇文章主要介绍了一个std :: shared_ptr <>的c - 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!
作为现在的解决方法,我建议采用这种方法,(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
以上是大佬教程为你收集整理的一个std :: shared_ptr <>的c - std :: tuple <>不起作用?全部内容,希望文章能够帮你解决一个std :: shared_ptr <>的c - std :: tuple <>不起作用?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。