3

I have a piece of code that compiles and works fine on Linux (Raspbian) but doesn't compile on windows (VS 17).

I am using CMAKE 3 for cross platform compiling, and like I said, I have no problem building this on Linux.

Here are the only CMAKE options that I am using:

cmake_minimum_required(VERSION 3.1)
project(Track)
set (CMAKE_CXX_STANDARD 11)
...
// The rest of the CMakeLists.txt has nothing fancy

But under windows (using VS 17 native compiler), there is a piece of code which doesnt even build and I don't get why. The error that I get is (sorry it is in french but pretty understandable I think):

error C2131: l'expression n'a pas été évaluée en constante    
note: échec en raison de l'appel d'une fonction indéfinie ou 'constexpr' non déclarée
note: voir l'utilisation de 'std::vector<ROI,std::allocator<_Ty>>::size'
error C3863: le type de tableau 'float ['fonction'+]['fonction'+]' n'est pas attribuable

And the (simplified) piece of code causing the error :

// Defined somewhere else
class ROI
{
}

class Tracker
{
public:
    void UpdateTrack(vector<ROI> new_roi)
    {
        // some code
        float match_table[new_roi.size() + 1][m_tracked_roi.size() + 1];  // COMPILE ERROR
        // some code
    }

private:
    vector<ROI> m_tracked_roi;
}

I think the problem is about the size of the array being known only at compile time or something like that, but it is possible with c++ now, and it works fine on Linux (by working I mean it builds and runs fine).

Can someone explain me what's goind on? and how to fix this on windows? (probably some additional CMake options?)

Thanks in advance

jww
  • 97,681
  • 90
  • 411
  • 885
Pierre Baret
  • 1,773
  • 2
  • 17
  • 35
  • 2
    Possible duplicate of [Enabling VLAs (variable length arrays) in MS Visual C++?](https://stackoverflow.com/q/5246900/608639) – jww Sep 15 '18 at 20:58

1 Answers1

5

Variable length arrays are not part of standard C++. Array bounds must be compile-time constant expressions.

GCC and Clang both provide VLAs as an extension, but VisualStudio does not. Use std::vector if you need a cross-platform non-constant length array.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • Thanks for your answer. That means that GCC and Clang are smarter compiler than VS ? Isn't this a bit of a shame? I know about std::vector (I use them all the time), but it is kind of a pain to declare 2D arrays with std::vectors. – Pierre Baret Mar 16 '18 at 02:02
  • No, it means that they implement a feature that is not part of the C++ language. – Miles Budnek Mar 16 '18 at 02:03
  • Is compile-time size arrays possible using std::array ? – Pierre Baret Mar 16 '18 at 02:06
  • `std::array`'s bound must be a compile-time constant just like a raw array. Its `size` method is `constexpr`, and so it is a valid compile-time constant expression for use in declaring another array (be that a `std::array` or a raw array). – Miles Budnek Mar 16 '18 at 02:12
  • I have read that VLA were in C99. WHat is going on with VS? – Pierre Baret Mar 16 '18 at 02:16
  • 4
    C and C++ are different languages. VLAs are part of C, but they are not part of C++. – Miles Budnek Mar 16 '18 at 02:17