Compile time array from C++ template parameter pack

Mathias

How can I at compile time create for example an std::array from a template parameter pack?

This shows what I need, but without a parameter pack.

template<typename T1, typename T2, typename T3>
struct ToInfoArray
{
    static constexpr std::array<Info, 3> value = { { T1::info, T2::info, T3::info } };
};

Live demo demonstrating the intended usage

Bonus question: Would you use std::array, array[] or std::initializer_list as type for InfoArray?

Piotr Skotnicki
template <typename... Ts>
struct ToInfoArray
{
    static constexpr std::array<Info, sizeof...(Ts)> value = { { Ts::info... } };
};

DEMO

Would you use std::array, array[] or std::initializer_list as type for InfoArray?

std::array<T,N>. It's an aggregate that behaves (with comparable performance) just like a regular array, but provides an additional interface for operating on its elements; itself, it's a copyable type.

std::initializer_list is not an option here for numerous reasons. Once it's used as a data member (with an in-class initializer), the elements it stores are invalidated after any of constructors' execution. It's not guaranteed to be a literal type, thus it can't be marked as constexpr. Its size is not usable (accessible) in constant expressions. It doesn't provide random access logic (without resorting to pointer arithmetic); the only way to enumerate its elements is to iterate from begin() to end(), which in addition yields pointers to constant items. std::initializer_list was designed to serve primarily as a function parameter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

simple way to convert parameter pack to array at compile time

From Dev

c++ Unpacking parameter pack from template arguments

From Dev

Is a template parameter evaluated at compile time?

From Dev

Is a template parameter evaluated at compile time?

From Dev

How to fill array with contents of a template parameter pack?

From Dev

Template, branching on length for array from compile-time initializer list

From Dev

Clang fails to compile parameter pack expansion using template metaprogramming

From Dev

Deduce template parameter pack from function call

From Dev

c++ template programming for detecting symmetric array at compile time

From Dev

C++ passing template parameter pack from one variadic template to another causes compiler error

From Dev

extract an array from another array at compile time using c++

From Dev

C++03: Is there a way to make a type that will compile to different types every time it is included in a template parameter?

From Dev

C++1y/C++14: Converting static constexpr array to non-type template parameter pack?

From Dev

How to extract a value from a variadic template parameter pack by index?

From Dev

How to extract params from template function parameter pack?

From Dev

How can a type be removed from a template parameter pack?

From Dev

How can a type be removed from a template parameter pack?

From Dev

C++ using a template parameter pack to invoke multiple templated functions

From Dev

Conditional Compile-time Type mapping based on template parameter

From Dev

Inspect template parameter pack in gdb

From Dev

Parameter pack with default template argument

From Dev

Cumulative Product of Template Parameter Pack

From Dev

Specialize on default template parameter pack

From Dev

Dynamically build a template parameter pack

From Dev

a variadic template as a template parameter without pack expansion

From Dev

Function template parameter pack not at the end of the parameter list

From Dev

C++: Forwarding the sum of parameter pack and a std::tuple/array

From Dev

Populate an array at compile-time from file

From Dev

C array alignment check at compile time

Related Related

  1. 1

    simple way to convert parameter pack to array at compile time

  2. 2

    c++ Unpacking parameter pack from template arguments

  3. 3

    Is a template parameter evaluated at compile time?

  4. 4

    Is a template parameter evaluated at compile time?

  5. 5

    How to fill array with contents of a template parameter pack?

  6. 6

    Template, branching on length for array from compile-time initializer list

  7. 7

    Clang fails to compile parameter pack expansion using template metaprogramming

  8. 8

    Deduce template parameter pack from function call

  9. 9

    c++ template programming for detecting symmetric array at compile time

  10. 10

    C++ passing template parameter pack from one variadic template to another causes compiler error

  11. 11

    extract an array from another array at compile time using c++

  12. 12

    C++03: Is there a way to make a type that will compile to different types every time it is included in a template parameter?

  13. 13

    C++1y/C++14: Converting static constexpr array to non-type template parameter pack?

  14. 14

    How to extract a value from a variadic template parameter pack by index?

  15. 15

    How to extract params from template function parameter pack?

  16. 16

    How can a type be removed from a template parameter pack?

  17. 17

    How can a type be removed from a template parameter pack?

  18. 18

    C++ using a template parameter pack to invoke multiple templated functions

  19. 19

    Conditional Compile-time Type mapping based on template parameter

  20. 20

    Inspect template parameter pack in gdb

  21. 21

    Parameter pack with default template argument

  22. 22

    Cumulative Product of Template Parameter Pack

  23. 23

    Specialize on default template parameter pack

  24. 24

    Dynamically build a template parameter pack

  25. 25

    a variadic template as a template parameter without pack expansion

  26. 26

    Function template parameter pack not at the end of the parameter list

  27. 27

    C++: Forwarding the sum of parameter pack and a std::tuple/array

  28. 28

    Populate an array at compile-time from file

  29. 29

    C array alignment check at compile time

HotTag

Archive