6

We know that C++ allows initialization of C-style arrays with zeros:

int a[5] = {0};
// or
int a[5] = {};

The same works for std::array

std::array a<int, 5> = {};

However, this won't work:

int a[5] = {33}; // in memory( 33, 0, 0, 0, 0 )
std::array<int, 5> = {33}; // in memory( 33, 0, 0, 0, 0 )

Is there any way to initialize the whole array with a non-zero value without using vector or algorhtm?

Maybe constexpr could help? What would be the best solution?

P.S.:

GCC offers this syntax

int a[5] = {[0 ... 4] = 33};

but I am not sure if it is valid for other compilers.

26
  • 1
    Maybe you need to tighten the requirements because int a[5] = {33, 33, 33, 33, 33}; Commented Apr 23, 2016 at 12:28
  • 2
    Well, Googling your exact title gives: 'About 1,470,000 results'. Maybe there is something in there that may be of use. Commented Apr 23, 2016 at 12:31
  • 3
    @LightnessRacesinOrbit Personally I prefer such a qualifier, it makes discussion clearer IMO. Commented Apr 23, 2016 at 12:35
  • 3
    @LightnessRacesinOrbit people, even programmers, rarely speak nor type namespaces in English. It is very common to discuss about vector and map and set, even though in C++, such things only exists in the std namespace. People discuss about arrays too, and depending on context, they may refer to either form of array or even a vector which is also an array of objects. Therefore there is a need for a way to disambiguate the term. Commented Apr 23, 2016 at 12:41
  • 4
    Changed from C to C-style Commented Apr 23, 2016 at 12:43

2 Answers 2

6

What do you have against <algorithm>? I think this is pretty clean:

int a[5];                                  // not initialized here yet
std::fill(std::begin(a), std::end(a), 33); // everything initialized to 33
Sign up to request clarification or add additional context in comments.

1 Comment

Nothing personal, it was more a theoretical question. We have a certain syntax which works for zeros, is there anything similar which might work for non-zeros.
3

I have some code around to achieve compile-time initialization of an std::array using template meta-programming (of course).

namespace impl {

    template <class SeqTy, size_t N, SeqTy FillVal, SeqTy... Seq>
    struct make_fill {
        using type = typename make_fill<SeqTy, N-1, FillVal, FillVal, Seq...>::type;
    };

    template <class SeqTy, SeqTy FillVal, SeqTy... Seq>
    struct make_fill<SeqTy, 0, FillVal, Seq...> {
        using type = std::integer_sequence<SeqTy, Seq...>;
    };

    template <class T>
    struct make_array;

    template <class SeqTy, SeqTy... Seq>
    struct make_array<std::integer_sequence<SeqTy, Seq...>> {
        static constexpr std::array<SeqTy, sizeof...(Seq)> value() { 
            return std::array<SeqTy, sizeof...(Seq)>{ {Seq...} };
        }
    };

} // end impl namespace

template <class SeqTy, size_t N, SeqTy FillVal = 0ul>
constexpr std::array<SeqTy, N> fill() {
    return impl::make_array<typename impl::make_fill<SeqTy, N, FillVal>::type>::value();
};

which you can use as follows:

std::array<size_t, N> ones = fill<size_t,N,1ul>();

I think you can easily adapt it if you don't want to use std::array

1 Comment

I must admit that my question was valid for std::array as well; the same initialization rules are applied there. Your answer is pretty much what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.