Search code examples
c++templatesbooststructboost-fusion

BOOST_FUSION_ADAPT_TPL_STRUCT and template array size


I am trying to iterate over a C++ template structure thanks to BOOST_FUSION_ADAPT_TPL_STRUCT. My structure contains fixed-size multidimensional arrays whose sizes are template parameters. If we consider Boost's example modified to fit my problem:

#include <iostream>
#include <string>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

// Example:
// http://www.boost.org/doc/libs/1_53_0/libs/fusion/doc/html/fusion/adapted/adapt_tpl_struct.html

namespace demo
{
    template<typename T, unsigned int SIZE1, unsigned int SIZE2, typename Name, typename Age>
    struct employee
    {
        Name name;
        Age age;
        T ar[SIZE1][SIZE2];
    };
}

// Any instantiated demo::employee is now a Fusion sequence
BOOST_FUSION_ADAPT_TPL_STRUCT(
    (T)(SIZE1)(SIZE2)(Name)(Age),
    (demo::employee) (T)(SIZE1)(SIZE2)(Name)(Age),
    (Name, name)
    (Age, age)
    (T, ar[SIZE1][SIZE2]))

int main()
{
    demo::employee<float, 2, 2, std::string, int> e;
    e.name = "Bob";
    e.age = 25;
    e.ar[0][0] = e.ar[1][0] = 0.1;
    e.ar[0][1] = e.ar[1][1] = 0.2;
}

The compilation fails. Moreover, it also fails if we only add an integer template parameter without even using it for the array size.

Is that even possible with BOOST_FUSION_ADAPT_TPL_STRUCT? If not, how should I go about this?


Solution

  • From doc:

    The sequence (template_param0)(template_param1)... declares the names of the template type parameters used.

    While you have non-type template parameter SIZE:

    template<typename T, unsigned int SIZE, typename Name, typename Age>
    struct employee
    

    You may convert it to type template parameter and use boost::mpl::int_ as wrapper for carry size.

    Now, your code is compiled.

    template<int Size>
    struct Array
    {
        template<typename T>
        struct Of
        {
            typedef T type[Size];
        };
    };
    
    namespace demo
    {
        template<typename T, typename SIZE, typename Name, typename Age>
        struct employee
        {
            Name name;
            Age age;
            T ar[SIZE::value];
        };
    }
    
    // Any instantiated demo::employee is now a Fusion sequence
    BOOST_FUSION_ADAPT_TPL_STRUCT(
        (T)(SIZE)(Name)(Age),
        (demo::employee) (T)(SIZE)(Name)(Age),
        (Name, name)
        (Age, age)
        (typename Array<SIZE::value>::template Of<T>::type, ar))
    
     //...
     demo::employee<float, int_<2>, std::string, int> e;