问题
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