Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ templates and derived classes

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;
};
like image 767
SunilS Avatar asked Dec 26 '12 12:12

SunilS


People also ask

Can template class be derived?

It is possible to inherit from a template class. All the usual rules for inheritance and polymorphism apply. If we want the new, derived class to be generic it should also be a template class; and pass its template parameter along to the base class.

What is the difference between class and template?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template.

What is a template in C programming?

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.

Where are template classes derived?

Explanation: Class derived template is derived from regular non-templated C++ class or templated class.


1 Answers

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.

like image 159
jrok Avatar answered Sep 30 '22 16:09

jrok