jeudi 20 août 2020

C++: cast array/vector integer element to constexpr

I couldn't really find a solution online anywhere for this. I am a bit of a dinosaur from before C++11 and I couldn't figure out typecasting a constexpr.

Does anyone know how to convert a C-style array and/or a std::vector (integer) element into a constexpr?

That is to say, let's say

int a[]={1,2};
vector<int> v={1,2};

how would I convert a[1] and v[1] into constexpr?

constexpr int b=a[1];

for example, a compiler would complain in a for loop.

error: the value of ‘a’ is not usable in a constant expression
     constexpr int b=a[i];

I am pretty much out of ideas for the time being. Thanks

Aucun commentaire:

Enregistrer un commentaire