samedi 24 septembre 2016

A template function crashes

I define following template function, which applies a function f on each element of an array

template <typename X, typename Y, size_t n>     
array <Y, n> lod (const function <Y (const X)> f, const array <X, n> w)
{
   array <Y, n> l;
   for (GLint i = 0; i = i + 1; i < n)
      l [i] = f (w [i]);
   return l;
}

As an example of f, I define a function "add a"

template <typename X>
function <X (const X)> ae (const X a)
{
   return [a] (const X x)
   {
      return x + a;
   };
}

Then I try to apply it

const array <ivec3, 12> oblad = {ivec3 (0, 2, 1), ivec3 (0, 3, 2), ivec3 (0, 1, 5), ivec3 (0, 5, 4),
                                ivec3 (1, 2, 6), ivec3 (1, 6, 5), ivec3 (2, 3, 7), ivec3 (2, 7, 6),
                                ivec3 (3, 0, 4), ivec3 (3, 4, 7), ivec3 (4, 5, 6), ivec3 (4, 6, 7)};

array <ivec3, 12> w = lod (ae (ivec3 (1)), oblad);

The program compiles just fine, however crashes upon execution.

Aucun commentaire:

Enregistrer un commentaire