C++ templates and derived classes
Asked Answered
N

4

6

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;
};
Novitiate answered 26/12, 2012 at 12:49 Comment(1)
Derived is a struct which has two base classes: T and Fallback.Giorgione
B
6

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.

Birchfield answered 26/12, 2012 at 12:53 Comment(4)
-1: doesn't really answer the OP question, he has a syntax misunderstanding, and asks about it to be explained.Dianoetic
When i try to compile using aix xlc 12.1, i get the following error. "YAMLTraits.h", line 264.20: 1540-0118 (S) A class name is expected in the base specifier. line 264 is "struct Derived : T, Fallback { };". thanksNovitiate
@user1768610 You're passing a non-class type to the template from which you can't derive.Birchfield
@user1768610 Please consider accepting this answer (by clicking the checkmark) if it helped.Kyoko
E
2

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.

Ectoblast answered 26/12, 2012 at 12:53 Comment(0)
K
1

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.

Kyoko answered 26/12, 2012 at 12:54 Comment(0)
D
1

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.

Dianoetic answered 26/12, 2012 at 12:54 Comment(2)
Stephane, When i try to compile using aix xlc 12.1, i get the following error. "YAMLTraits.h", line 264.20: 1540-0118 (S) A class name is expected in the base specifier. line 264 is "struct Derived : T, Fallback { };". any idea what could be the reason ?Novitiate
I suppose the compiler doesn't grasp one of the names the error being " a class name is expected in the base specifier ". Maybe you could change the code and put T and Fallback on two different lines, so as the compiler tells you clrearly what name it is angry with :-) Possibly if you gave him an int as the T parameter of the template and it may not accept it as a base class type... just a possibility.Dianoetic

© 2022 - 2024 — McMap. All rights reserved.