-1

How to detect whether a class T has at least 1 member that are pointers?

#include <iostream>
class B{
    int k;             //no pointer  -> false
};
class C{
    void* x;           //pointer     -> true
};
class D{
    int someRandom;
    C* aBadPractice;   //pointer     -> true
};
template<class T> bool hasPointer(){  ??
    ???
};
int main(){
    std::cout<<check<B>()<<std::endl; //false
    std::cout<<check<C>()<<std::endl; //true
    std::cout<<check<D>()<<std::endl; //true
}

I want to use it to roughly check that every type of component in my Entity-Component-System is a custom POD (plain old data).

If it is violated, it will alert programmer about potential mistake.

I can't use std::is_pod because its criteria is so much different from mine.

Main objective / criteria :-

  • Can be slow (I don't mind performance).
  • Compile-time or Runtime check are both OK.
  • It must be non-intrusive. (B C & D should not be edited)
  • If it helps, it is ok to enforce B C & D to inherit from a common class.

Side objective :- (not important)

  • can detect private field
  • detect inherited field
  • detect a field that is a type that has pointer inside it

Here are similar questions (they are too different though) :-

I personally don't think it is possible.
A solution like "It is impossible" is also a valid answer.

Edit

My underlying problem is identifying whether I can memcpy && omit destructor of them. I want to relocate them easily and safely - like this : Can I use memcpy in C++ to copy classes that have no pointers or virtual functions.

Now I believe the solution are somethings around std::is_trivially_destructible and std::is_trivially_copyable. I will investigate more about them. I hope they can check whether my class contains any smart pointer.

Thank every comments.

cppBeginner
  • 1,114
  • 9
  • 27
  • @user0042 I had put that link and clarified it - that question is "check whether a field with a ***certain name*** is a pointer." It is different from mine. – cppBeginner Aug 13 '17 at 12:25
  • @Ron I don't think it can be done batch-like though (e.g. `std::is_pointer` is false) . – cppBeginner Aug 13 '17 at 12:32
  • 7
    This cannot be done in C++. C++ does not work this way. What is the real problem are you trying to solve? No, not the problem of determining if a class a has pointer member, but whatever problem you think the solution is to determine if a class has a pointer member. – Sam Varshavchik Aug 13 '17 at 12:36
  • What you're looking for is reflection which looks possible to an extent in c++14, check this talk: https://youtu.be/abdeAew3gmQ – Serge Kireev Aug 13 '17 at 12:42
  • 1
    _I personally don't think it is possible_. It isn't indeed. This smells a lot like an XY-problem. – skypjack Aug 13 '17 at 12:55

1 Answers1

3

is_trivially_copyable is likely the attribute you want. Even if the class contains a pointer, creating a shallow copy can be the right thing. If not, it will have to have a non-trivial copy constructor and/or assignment operator - and thus not be trivially copyable.

However, if you just copy or assign an object that is trivially copyable the compiler will very likely generate code similar to memcpy anyway. There is generally no need to try to optimize this in user code.

Likewise, if there is nothing to destroy the default destructor will be empty and a clear candidate to be removed by the optimizer. For example, consider what happens when an empty destructor is inlined.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203