简体   繁体   中英

std::array derived class aggregate initialization

I am making a small helper class that derives from std::array . The constructor does not inherit, obviously, and it is that which is responsible for brace-initialization; for example:

template<typename T, size_t size>
struct foo : std::array<T,size>
{
     foo(int a, int b)
     : std::array<T,size>{a,b}
     {
          //nothing goes here since constructor is just a dummy that 
          //forwards all arguments to std::array constructor
     }
}

int main()
{
     foo<int,2> myobj = {1,2}; //brace initialization calls custom constructor with inner elements as arguments
}

The amount of arguments has to match exactly, so I am leaning towards using something like a variadic function argument in the constructor (since I am not only going to be using 2 elements in the array every single time). Using this, how would I forward the variadic argument pack to the std::array constructor? I am open to other methods of brace initialization that allow forwarding to the std::array constructor.

Note: std::initializer_list requires runtime initialization, and i am looking for a compile time/constexpr compatible method. Thank you.

You can use a perfect-forwarding constructor:

template<class... U>
foo(U&&... u)
    : std::array<T, size>{std::forward<U>(u)...}
{}

I don't think that inheriting from a standard container is a good idea.

Anyway...

You can use variadic templates, perfect forwarding and also SFINAE to impose that the number of arguments is exactly size .

You can also make constexpr the foo constructor so you can make constexpr foo objects.

By example

#include <array>
#include <type_traits>

template <typename T, std::size_t S>
struct foo : public std::array<T, S>
 {
   template <typename ... As,
             typename std::enable_if<sizeof...(As) == S>::type * = nullptr>
   constexpr foo (As && ... as)
      : std::array<T, S>{ { std::forward<As>(as)... } }
    { }
 };

int main ()
 {
   //constexpr foo<int, 2u> myobj1 = {1}; // compilation error
   constexpr foo<int, 2u> myobj2 = {1, 2}; // compile
   //constexpr foo<int, 2u> myobj3 = {1, 2, 3}; // compilation error
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM