samedi 22 mai 2021

Selecting between valid and non-valid non-type parameter pack

I'm trying to replicate the answer to this question: Selecting between valid and non-valid types but I can't figure out how to achieve this with a non-type template parameter pack.

For context, I'm trying to implement a templated multidimensional array

template<typename T, std::size_t CurrentDim, std::size_t... Dims>
class Array {

In the class, I'm trying to create a array of type T if Dims is empty and an an array of type Array<T, Dims...> if Dims is not empty. For example:

using SubType = std::conditional<sizeof...(Dims) == 0, T, Array<T, Dims...>>;        
SubType array[CurrentDim];

The problem is that if Dims is empty, the conditional fails with

error: too few template arguments for class template 'Array'

...since the second part of the conditional, which is invalid, is evaluated even though the check is false. Does anyone know how to achieve this? I know I could specialize my Array class but I believe that would require me to duplicate all my code?

Here's another related question: std::conditional compile-time branch evaluation

Aucun commentaire:

Enregistrer un commentaire