Search code examples
c++boost-fusionboost-preprocessor

BOOST_PP_REPEAT with boost::fusion::size


I want to iterate in compile time over struct and write to output number of iteration. Just to mention - in real case I will pass some more parameters in data.

#include <iostream>
#include <string>
#include <vector>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/size.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

struct MyStruct
{
    int x;
    int y;
};

BOOST_FUSION_ADAPT_STRUCT(
    MyStruct,
    (int, x)
    (int, y)    
    )

#define PRINT(unused, number, data) \
    std::cout << number << std::endl;

int main()
{
    MyStruct s;

    std::cout << boost::fusion::size(s) << std::endl;
    //line below works - it iterate and write output
    BOOST_PP_REPEAT(2, PRINT, "here I will pass my data")

    //this won't compile 
    //BOOST_PP_REPEAT(boost::fusion::size(s), PRINT, "here i will pass my data")
}

How to fix problematic line so it will work when I will add more members in structure? I need solution for C++03 :(


Solution

  • Here is exact solution for this problem (asked more general question later, and found answear which solve this problem too): https://stackoverflow.com/a/31713778/4555790