c++ – Using boost :: MPL in multiplication table computation

Question:

The task is simple: for a given n, calculate the elements of the table. multiplication: ij (i = 1 … n, j = 1 … n). Print the resulting table.
But it is complicated by the fact that all the calculations, as well as the output of the table, need to be done using the boost :: MPL library, which I don't know much about).
Wrote code like this:

const int n = 5;
typedef range_c<int, 1, n> numbers;
typedef vector<int_<3>> data;       // int_<3> для примера
template <class T> struct wrap{};
typedef mpl::fold<
    numbers,
    data,
    mpl::push_back<_1, mpl::plus<int_<3>, back<_1>>>
>::type fold_line;

struct print_value {
    template <class col>
    void operator()(wrap <col>) const {
        std::cout << std::setw(4) << col::value;
    }
};

template <class row> void print_line() {
    mpl::for_each< row, wrap <_> >(print_value());
    std::cout << std::endl;
};

struct line {
    void operator()() const {
        print_line<fold_line>();
    }
};

int main() {
    std::cout << "Table " << n << "x" << n << std::endl;
    mpl::for_each<numbers>(line());
    system("pause");
    return 0;
}

But the error takes off:
error C2064: the result of evaluating the fragment is not a function that takes 1 arguments boost \ mpl \ for_each.hpp

In addition, if I understood correctly how it works, the line "3 6 9 12 15" should be displayed here 5 times, that is, only one line of the table. And how to make it possible to transfer different data to mpl :: fold, that is, not vector<int_<3>> , but vector<int_<n>> , where n is the current row number of the table, and in a line of code

mpl::push_back<_1, mpl::plus<int_<3>, back<_1>>>  

Instead of int_<3> also use something like int_<n> ?

Answer:

You have functions for printing strings, elements, etc. do not take any parameters. How do they know what to print?

Polymorphism by the type of a function parameter is implemented through a template

struct line {
  template< typename T >
  void operator()(T &v) const {
      print_line(v);
  }
};

Any parameter can be passed to such a function int_<n>

Scroll to Top