Question

I am trying to understand the following code. Derived is a derived structure from T and what does "," means and then Fallback {}

template <class T>
struct has_FlowTraits<T, true>
{
  struct Fallback { bool flow; };
  struct Derived : T, Fallback { };   //What does it means ?

  template<typename C>
  static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];

  template<typename C>
  static char (&f(...))[2];

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

Solution

It's an implementation of Member Detector Idiom. It uses SFINAE to check whether type T has got a member called flow.

Edit: The comma part you're asking about is multiple inheritance. Struct Derived is (publicly) inheriting from both T and Fallback.

OTHER TIPS

It's just multiple inheritance. The following is a Derived that is derived from T (and provides no further definition):

struct Derived : T { };

And the following is a Derived that is derived from both T and Fallback:

struct Derived : T, Fallback { };

That is, Derived will inherit the members of T and the members of Fallback. In this case, since Derived is a struct, the inheritance is by default public inheritence.

It means:

inside the definition of has_FlowTraits struct, you also define a new struct which is called Derived.

You say that this struct Derived is inheriting the type T, and the type Fallback. ( If you look at the line before, the struct Fallback has just been defined).

the {} simply means that there are no more details of implementation. No more method or attribute definition is needed for this type to be useful.

The comma means it derives either publicly or privately (depending on whether the T or Fallback is a struct or class) from those two classes. The comma simply includes those classes as those from which Derive will derive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top