Captain'Flam
Captain'Flam

Reputation: 537

initialize an array using const values using a template compilation-time computation

I'm trying to write something like this :

template <typename type,int p,int q = 1> struct const4array
    {
    static const type value [] = { type(p)/type(q) , type(p)/type(q) , type(p)/type(q) , type(1) } ;
    };

double xx [] = { 0.5 , 0.5 , 0.5 , 1 } ;
double yy [] = const4array<double,1,2>::value ; // I would like to have : xx == yy

I think this code is easy to understand for a human developer, but apparently not for the compiler (it returns many errors).

Is it possible to do what I want, and if it is, how to do it properly? (I found many other questions that looks like this one, but not close enough to match mine).

Thanks in advance!

Upvotes: 0

Views: 32

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409472

Plain C-style arrays can only be initialized using {} syntax, they can't be copy-initialized from other arrays. You can however use objects emulating or wrapping arrays (like std::array) as those can be copied and used for initialization.

Upvotes: 1

Related Questions