Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation on Member Detection code

Tags:

c++

It's not the concept as a whole, but rather one of the methods it uses to determine if a class has an n data-member. Here is the full code; an ordinary use of SFINAE for member detection.

template <typename T>
struct has_X {
    struct Fallback { int X; };
    struct Derived : T, Fallback {};

    template <typename U, U> struct S;

    template <typename C> static char (&f(S<int Fallback::*, &C::X> *))[1];
    template <typename C> static char (&f(...))[2];

    public:
        const static bool value = sizeof(f<Derived>(0)) == 2;
};

The part where Derived inherits from both Fallback and T confuses me because when we do the overload of f, &C::X is &Derived::X. But shouldn't this overload always be chosen because isn't Derived guaranteed to have X since it inherits from Fallback which has that data-member?

Maybe I'm overlooking something. However, this single piece of code has shown and taught me things I never knew, so maybe there is something to this. What I would expect is for that overload to always be chosen (not the one with the ...) because Derived should always have X since it inherits from Fallback. But this is not the case. Can someone please explain why?

like image 769
David G Avatar asked Dec 27 '12 22:12

David G


People also ask

What is person detection?

Person Detection uses computer vision to detect people within your video. Through this feature, you can customize your video and alert preferences to decide whether you want to see video and alerts for all motion, people only, or have them suppressed.

What is a good mean IOU score?

An Intersection over Union score > 0.5 is normally considered a “good” prediction.

How do object detection models work?

What is object detection? Object detection is a computer vision technique that works to identify and locate objects within an image or video. Specifically, object detection draws bounding boxes around these detected objects, which allow us to locate where said objects are in (or how they move through) a given scene.


1 Answers

Fallback has one data member named X, but Derived will have two if T also has a member named X, in which case Derived::X cannot be taken unambiguously. So if T does not have X, the first overload is used, and if T has X, the second more general version is used. This is why you can tell these cases apart depending on the size of their return types.

like image 86
Anders Johansson Avatar answered Oct 06 '22 16:10

Anders Johansson