mardi 21 avril 2015

Variable length arrays in g++?

Before you downvote (I know, lousy title), read first...

In this answer http://ift.tt/1DKvoWM the user reports that his g++ version compiles VLA's, as well as in this question Are variable length arrays there in c++? . On my compiler this also works (has that extension), but if I want to declare such an array as a class member, such as position and velocity in

class PSO
{
    private:
        static double * data;
        static int len;
        static int dim;
        double position [dim];
        double velocity [dim];

        double get_min();
        double get_max();
        void copy_data(double data *);

//...

it does not compile. What is the reason for this? Why would the compiler extension allow VLA in a main function, but not in the declaration of a class member?

Oh and for goodness' sake, if you want to vote this as duplicate, please direct me to an answer that **actually answers this. In other words, it has to do with why it would compile in a main function, but not in a class member declaration **. I also ask this question to get a fuller understanding of the underlying mechanics of C++. Thank you.

Addendum I am using gcc 4.8.2

Aucun commentaire:

Enregistrer un commentaire