Ram
Ram

Reputation: 3153

Variadic templates : Creating a compile time array

I have a function that I want to use to create an array at compile time.

template<typename... uint32_t> 
static constexpr auto AddressArray(uint32_t... ns) { 
    return std::array<uint32_t, sizeof ...(uint32_t)>{ ns... }; 
}

when I use this code, I get a compiler error

`compiler is out of heap space.` 

What am I doing wrong?

Upvotes: 0

Views: 142

Answers (1)

bartop
bartop

Reputation: 10315

Instead of giving a pack of types, it would be better to give a type for array and pack for values:

template<typename T, T ...vals> 
static constexpr auto AddressArray() { 
    return std::array<T, sizeof...(vals)>{ vals... }; 
}

Example usage:

auto array = AddressArray<int, 1, 2, 4, 5>();

Upvotes: 1

Related Questions