unqiue_ptr<void> 谁践踏了优雅 2023-01-03 04:16 141阅读 0赞 #include <iostream> #include <string.h> #include <unordered_map> #include <chrono> #include <vector> using std::string; string handle_; void set_handle(string handle) { handle_ = std::move(handle); } #include <iostream> using namespace std; auto func = [](int x, int y) { return x + y; }; inline void test(){ printf("this is a inlint test"); } struct A { int a[10]; }; struct B { int *b [10]; }; enum T { A=12, B=13 }; template<T A> struct EnumToDataType { }; void deleter (void const * data) { int64_t const * p = static_cast<int64_t const*>(data); std::cout << *p << " located at " << p << " is being deleted"; delete p; } int main() { void * b = malloc(8); int64_t c = 12; memcpy(b, &c, sizeof(c)); std::unordered_map<std::string, std::unique_ptr<void, void(*)(void const*)> > a ; // a.insert(std::pair<std::string, unique_ptr<void>>{"12", static_cast<void*>(&b)}); // // a.emplace("13", static_cast<void*>(&b)); // { // std::unique_ptr<void, void(*)(void const*)> d(b, deleter); typedef std::unique_ptr<void, void(*)(void const*)> UniqueV; // auto e = std::pair<std::string, UniqueV>("12", {b, deleter}); // a.emplace( // "12", d); std::vector<UniqueV> uni_vec; uni_vec.push_back(UniqueV{ b, &deleter}); auto it = uni_vec.begin(); std::cout << "it:" << *static_cast<uint64_t*>(it->get()); uni_vec.erase(it); // uni_vec.pop_back(); // } std::cout << "\n*b" << *static_cast<uint64_t*>(b); return 0; }
还没有评论,来说两句吧...