Reputation: 2325
My class needs to impose an upper limit on the number of elements of one of its data members, which happens to be a container.
One of the constructors of my class takes an std::initializer_list as a parameter to initialize that container data member.
A unit test needs to verify that the upper limit is in place. For this I need to try to invoke my constructor with an initializer list with 1025 elements.
Is there a better (= more readable) way to create this 1025-element initializer_list, than having a braced literal list of 1025 elements in the test code?
(The data type of the elements is always unsigned short (uint16_t), my class is not a template.)
Upvotes: 2
Views: 295
Reputation: 2515
If you go for template recursion, or fancy "repeat" macros, you are possibly going to be hit by static recursion limits, which is going to complicate your implementation even more than usual.
With this in mind, I think the simplest and best approach is just to define some nested macros, i.e.
#define ZEROS_10 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
#define ZEROS_50 ZEROS_10, ZEROS_10, ZEROS_10, ZEROS_10, ZEROS_10
#define ZEROS_250 ZEROS_50, ZEROS_50, ZEROS_50, ZEROS_50, ZEROS_50
#define ZEROS_1K ZEROS_250, ZEROS_250, ZEROS_250, ZEROS_250
FooType f{ ZEROS_1K, ZEROS_10, ZEROS_10, 0, 0, 0, 0, 0 };
Upvotes: 3