Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(C++14) Array of lambdas: error: 'name' declared as array of 'auto'

I'm having a bad time figuring this error out. I'll admit, I'm new to c++ and my difficulty comes from not understanding the error message.

Here's the code:

auto selectionFuncs[8] =
{
    [&](const Vector3& min, const Vector3& max) 
    { 
      return max.x_ == seamValues.x_ || max.y_ == seamValues.y_ || max.z_ == seamValues.z_;
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.x_ == seamValues.x_; 
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.z_ == seamValues.z_; 
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.x_ == seamValues.x_ && min.z_ == seamValues.z_; 
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.y_ == seamValues.y_; 
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.x_ == seamValues.x_ && min.y_ == seamValues.y_; 
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.y_ == seamValues.y_ && min.z_ == seamValues.z_; 
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.x_ == seamValues.x_ && min.y_ == seamValues.y_ && min.z_ == seamValues.z_; 
    }
};

And here's the error:

error: ‘selectionFuncs’ declared as array of ‘auto’

From googling around, it seems using auto in this instance is not allowed in C++11 but it should be in C++14, however I must be declaring it wrong somehow and can't figure it out.

Help is very much appreciated, thank you!

like image 404
JustHeavy Avatar asked Dec 11 '22 06:12

JustHeavy


1 Answers

The C++ language forbids having arrays declared with auto. You have two good options: function pointers and even better - std::function. Something like this:

std::function<bool(const Vector3&, const Vector3&)> selectionFuncs[8] =
{
    [&](const Vector3& min, const Vector3& max) 
    { 
      return max.x_ == seamValues.x_ || max.y_ == seamValues.y_ || max.z_ == seamValues.z_;
    },

    [&](const Vector3& min, const Vector3& max) 
    { 
      return min.x_ == seamValues.x_; 
    },

    // ...
};

Don't forget to #include <functional>. Then you just use the elements of the array like any other functions.

like image 199
DeiDei Avatar answered May 23 '23 05:05

DeiDei