3

I need a constant array of constants, which its constants (the elements of the constant array of constants) can be used where only a compile time constant can be used, like array length definitions.

E.g:

int a[ my_const_array_of_const[0] ]; int b[ my_const_array_of_const[1] ]; template<int p> foo() { ... }; foo< my_const_array_of_const[2] >(); 

I have tried solutions form other answers, but they were not "constant" enough to the compiler not give an error when using them on above situations.

How can I create the "my_const_array_of_const" constant to compile in such situations?

I need it to configure a High-Level Synthesis (HLS) design. For HLS C++ syntax is restricted. No dynamic memory is allowed, hence I need to use static arrays. Besides, all compilation time constants may be used to optimize the hardware accelerator (that is the reason to use template parameters instead of variables).

4
  • How about a macro? Commented Jun 16, 2017 at 15:32
  • As far as I know, macros #define can only "define" scalars. But actually I hace tried something like: #define element(n,d) ==(n) ? d : #define my_const_array(i) (i) element(0,1) (i) element(1,2) (i) element(2,5) 0 The problems are: (1) is not very elegant, (2) the lengtht is limited, (3) and it leads to very long expressions Commented Jun 16, 2017 at 16:57
  • Why does it need to be an array? Commented Jun 16, 2017 at 16:58
  • Because I need to configure a configurable number of objects. I want to keep the configuration parameters of these objects on an array, so I can acces to param_a[0] to object 0, param_a[1] to object 1, etc... Commented Jun 16, 2017 at 17:02

1 Answer 1

2

You could use constexpr (since C++11), which guarantee that the value of the element of the array could be evaluated at compile time. e.g.

constexpr int my_const_array_of_const[2] {1, 2}; 

LIVE

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it may work on C++11. However, I am guessing that my tool for HLS (Vivado) uses an older version of C++. It gives me the following error: ../../../src/main.cpp:8:1: error: 'constexpr' do not name a type. ../../../src/main.cpp:8:1: note: C++0x 'constexpr' is only available with -std=c++0x o -std=gnu++0x
Is there another way to do it with earlier vesrions of C++?
@Ojos AFAIK, no. Could you compile with the option -std=c++0x
The simulation (i.e. the program compiled and execute on the PC) runs fine with that compiler option (i.e. with the C++11 features). However, the synthesis cannot use the C++ features. If there is no other way to do it, I will need to wait until the synthesis tool implement the new C++11 features

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.