The task is simple: for a given n, calculate the elements of the table. multiplications: ij (i = 1 ... n, j = 1 ... n). Print the resulting table.
But it is complicated by the fact that all the calculations are needed, as well as the output of the table using the boost :: MPL library, which I do not understand well).
I wrote this code:

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 the fragment calculation is not a function that takes 1 argument boost \ mpl \ for_each.hpp

In addition, if I correctly understood how it works, then the line "3 6 9 12 15" should appear here 5 times, that is, only one line of the table. 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 the line of code

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

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

  • 2
    Please attach the full source, please - Alexey Lobanov
  • one
    why Boost.mpl? There are constexpr functions - Abyx

1 answer 1

You have functions for printing lines, elements, etc. do not accept any parameters. How do they know what to type?

Polymorphism on the type of the function parameter is implemented through the template.

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

You can pass any parameter int_<n> such a function int_<n>