mercredi 6 septembre 2017

C++11: Recursion in a stacked variadic struct (I get an linker error)

I wanted to implement a template recursion algorithm where I forward two variadic parameters and decrement the variable K. I thought that the easiest approach is to stack two structs definitions. However, I get a linker error if I try to make an debug output on a constexpr. A similar approach code seems to work if I just do it in a non-stacked struct.

: In function `void detail::L1<3ul, 4ul, 3ul, 2ul, 1ul>::L2<2ul, 3ul, 4ul, 5ul>::run<int, int, int, int>(int, int, int, int)':
: undefined reference to `detail::L1<3ul, 4ul, 3ul, 2ul, 1ul>::L2<2ul, 3ul, 4ul, 5ul>::dim_array'
: In function `void detail::L1<2ul, 4ul, 3ul, 2ul, 1ul>::L2<2ul, 3ul, 4ul, 5ul>::run<int, int, int, int>(int, int, int, int)':
: undefined reference to `detail::L1<2ul, 4ul, 3ul, 2ul, 1ul>::L2<2ul, 3ul, 4ul, 5ul>::dim_array'
: In function `void detail::L1<1ul, 4ul, 3ul, 2ul, 1ul>::L2<2ul, 3ul, 4ul, 5ul>::run<int, int, int, int>(int, int, int, int)':
: undefined reference to `detail::L1<1ul, 4ul, 3ul, 2ul, 1ul>::L2<2ul, 3ul, 4ul, 5ul>::dim_array'

Code

template <size_t K, std::size_t... IDs>
struct L1 {
    template <std::size_t... DIMS>
    struct L2 {
        static constexpr std::array<size_t, sizeof...(DIMS)> dim_array = ;
        static constexpr std::array<size_t, sizeof...(IDs)> id_array = ;

        static void run() {
            //const auto arg_array = detail::param_array<ARGS...>();
            constexpr size_t n = dim_array[K];
            constexpr size_t k = id_array[K];

            for(int i = 0; i < sizeof...(DIMS); i++) {
                std::cout<<dim_array[i]<<" ";
            }
            std::cout << std::endl;

            L1<K-1, IDs...>::template L2<DIMS...>::run();
        }
    };
};

template <std::size_t... IDs>
struct L1<0, IDs...> {
    template <std::size_t... DIMS>
    struct L2 {
        static constexpr std::array<size_t, sizeof...(DIMS)> dim_array = ;
        static constexpr std::array<size_t, sizeof...(IDs)> id_array = ;

        static glm::vec3 run() {
            //const auto arg_array = detail::param_array<ARGS...>(args...);
            constexpr size_t n = dim_array[0];
            constexpr size_t k = dim_array[0];
        }
    };
};

Exec with

L1<3, 4,3,2,1>::template L2<2,3,4,5>::run(1,2,3,4);

Works:

template <std::size_t... DIMS> // Define dimensions of the ctrl point grid. E.g. 4x4x4
struct works {
  static void run() { 
    static constexpr std::array<size_t, sizeof...(DIMS)> dim_array = ;
    for(int i = 0; i < sizeof...(DIMS); i++) {
      std::cout<<dim_array[i]<<" ";
    }
    std::cout << std::endl;
  }
};

Aucun commentaire:

Enregistrer un commentaire