lundi 22 janvier 2018

Elegant way to initialize array of structs in C++98

I am using gcc version 4.9.2 If I compile using the compiler flag -std=c++0x the following code compiles OK.

#include <iostream>
#include <vector>

using namespace std;
typedef struct
{
    vector<int> a;
    int b;
} MYTYPE;

int main(void)
{
    MYTYPE test[]=
    {
        { {1,2,3},4},
        { {5,6},7},
        { {},8}
    };
}

If I remove the -std=c++0x flag then the compiler reports:

error: could not convert ‘{1, 2, 3}’ from ‘’ to ‘std::vector’

What is an elegant way to initialize test[] ?

Aucun commentaire:

Enregistrer un commentaire