Is there a way of applying a function to each member of a struct in c++?

后端 未结 13 1145
轻奢々
轻奢々 2021-01-13 02:31

You have a simple struct, say:

typedef struct rect
{
    int x;
    int y;
    int width;
    int height;
}
rect;

And you want to multiply

13条回答
  •  忘掉有多难
    2021-01-13 03:04

    Boost.Fusion offers the BOOST_FUSION_ADAPT_STRUCT macro, which can turn any struct into a Fusion sequence, which Fusion can then iterate over (as demoed in the Quick Start).

    Another option is to use a third-party C++ reflection library like Reflex to iterate over a struct's members.

    However, all of this may be overkill for your purposes; manually listing each of the class's members may be less elegant, but it's much simpler, and simplicity in coding is important.

提交回复
热议问题