Coder Social home page Coder Social logo

foreachmember's Introduction

Iterate over members in struct, no hacks required! Travis Build Status

Usage

struct T {
  T1 t1;
  T2 t2;
  T2 t3;
  ...
  TN tN;
} t;

Then foreachMember(t, callback); is the equivalent to the following code:

callback(t.t1);
callback(t.t2);
callback(t.t3);
...
callback(t.tN);

Example

Try it online: https://godbolt.org/z/5n3Zjz

struct A {
  char c;
  std::string s;
  int i;
} a;

a.i = 42;
a.s = "test";
a.c = 'c';

foreachMember(a, [](auto&& v) {
   std::cout << v << ", ";
});

// this code prints "c, test, 42, "

Requirement

  1. T must be standard layout type (https://en.cppreference.com/w/cpp/named_req/StandardLayoutType)
  2. T should be aggregate (https://en.cppreference.com/w/cpp/language/aggregate_initialization)
  3. All non-static data member should be default constructible
  4. All non-static data member must be move constructible
  5. All non-static data member must not have template converting constructor
  6. T must not have bit field (https://en.cppreference.com/w/cpp/language/bit_field)
Note: if T has bit field, using foreachMember results undefined behavior.

otherwise, it should either work, or give you compile-error.

Figuratively speaking, here is an example which satisfies all requirements

class T {
 public:
  double d;
  const std::vector<U> v; // U does not matter, const is fine
  std::unique_ptr<U> p; // non-copyable is fine
  U* ptr; // pointer is fine

  U foo(std::string); // member function is fine
  void* operator&(); // overloading is fine

  struct A {
    std::optional<int> u;
  } a; // any standard layout type is fine
  std::thread t; // most types under std:: are fine, only few exceptions

 private:
  static U u; // static private is fine, it'll be ignored
};

Here is a counterexample which does not satisfy some requirements, therefore it can't be used by foreachMember

struct T : std::set<int> // base class is not supported
{
  T() {} // any custom ctor is not supported, even it's empty
  const int& ref; // reference is not default constructible
  virtual void foo(); // virtual function breaks standard layout

  std::condition_variable cv; // non-moveable is not supported

  // All members must be standard layout type.
  // These are counterexamples that can't be data member.
  std::exception e;
  std::fstream f;
  std::function<void()> func;
  std::tuple<int,int> t;
  struct M { virtual ~M(); } m;

  int x = 10; // default member initializer is not supported

 protected:
  int x; // private/protected non-static data member is not supported
};

foreachmember's People

Contributors

mizuchi avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lvenerosy joluxer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.