Is there a way to determine whether an object has at least one of its members on the heap?
I am trying to be able to distinguish objects like std::string or std::vector or std::list (yeah mostly containers) from all other types (unfortunately even containers with a single element are within my "scope of interest")
I am trying to do something like the following:
struct foo
{
private:
int * _ptr;
public:
foo() : _ptr(new int) {};
~foo() { delete _ptr; };
};
struct bar
{
private:
int val;
};
template <typename T>
void func(T val)
{
if constexpr (std::is_class_v<T>)
{
std::cout << std::setw(20) << typeid(T).name() << " is a class type." << std::endl;
if (/* determine if foo has any heap allocations */)
{
// Do something #1.
std::cout << std::setw(20) << typeid(T).name() << " does allocate on heap." << std::endl;
}
else
{
// Do something #2.
std::cout << std::setw(20) << typeid(T).name() << " does NOT allocate on heap." << std::endl;
}
}
else
{
// Do something #3.
std::cout << std::setw(20) << typeid(T).name() << " is NOT a class type." << std::endl;
}
}
int main()
{
func(foo()); // foo does allocate on heap
cout << endl;
func(bar()); // bar does NOT allocate on heap
cout << endl;
};
classes foo and bar are just examples, and the function func() has to perform functionality a bit different than couting to console.