how to define a good/perfect New function to init any class/struct in C++?

。_饼干妹妹 提交于 2020-04-17 21:53:36

问题


    struct object {
        void Name() {}
    };
  struct ABC:object { //Note:did not declare any constructor and base class is object
    std::byte a;
    const std::string* b;
    int c;
  }
struct BCD { //Note:did not declare any constructor and none base class 
    std::byte a;
    const std::string* b;
    int c;
  }

i want the New function accept:

New<ABC>({12,"first",123});
New<ABC>(a,b,c);
New<BCD>({12,"first",123});
New<BCD>(a,b,c);

this is my New function define:

    template <class _Ty, class... _Types, std::enable_if_t<!std::is_array_v<_Ty>, int> = 0>
    auto New(std::initializer_list<std::any>&& _Args) {
        //i can get type informationof the _Ty For use GC  at here 

        return  new _Ty(std::forward<_Types>(_Args)...);
        //if need also can be record the pointer For use GC at here
    }

but i cound'nt got the correct result;

I use the latest VC ++ (vs2019) So you can use the tool to provide the latest C ++ language standard.

Why i need this function: i want add GC into C++ ,but i need take more type info Before allocating memory. Actually I want to get the type information before calling operator new.This is a big plan, I think I can get the unique ID of the information (or use reflection), whether the scan type contains a set of pointers (references) and their offsets. This provides the GC with a run-time memory scan. Improve conservative GC performance. I just started planning.

来源:https://stackoverflow.com/questions/61116062/how-to-define-a-good-perfect-new-function-to-init-any-class-struct-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!