xxx_coder_noscope
xxx_coder_noscope

Reputation: 85

Check if static property exists on the class?

How can i check, that some class, passed as a template argument, have some static property on it? Like ::size

  template<class Header>
  class Reader {
// e.g.  Header::size
  };

My concept is that each Header class must implement a size property that will be read by the Reader class. The Reader class, in turn, will read the number (size) of bytes from the file, and then pass the buffer to the Header ctor.

Upvotes: 1

Views: 956

Answers (1)

Fatih BAKIR
Fatih BAKIR

Reputation: 4735

Simply use a concept:

template <class T>
concept HasSize = requires(T) {
    T::size;
};

template <HasSize Header>
struct Reader {
    // Use Header::size
};

Upvotes: 3

Related Questions