← Samples

Pass arrays

12345678910111213141516171819#include <array> #include <experimental/dynarray> void compile_time(std::array<int, 3> arr) { } void run_time(std::experimental::dynarray<int> arr) { } int main() { std::array<int, 3> arr = {4, 8, 15}; compile_time(arr); compile_time({16, 23, 42}); std::experimental::dynarray<int> dynarr = {1, 2, 3}; run_time(dynarr); run_time({1, 2, 3, 4, 5}); }

This sample is licensed under the CC0 Public Domain Dedication.


Pass fixed-size arrays to and from functions.


Built-in array types are not copyable, so cannot be passed to and from functions by value. The traditional approach is to pass both a pointer to an array’s first element and the size of the array. However, C++ provides class types that represent copyable fixed-size arrays.

We define the function compile_time, on lines 4–5, to have a parameter of type std::array, which represents compile-time fixed-size arrays. This means that the size must be known at compile time. A template may be used to instantiate functions for different std::array sizes. Lines 12–14 demonstrate passing arrays of size 3 to this function.

On lines 7–8, we define function run_time to have a parameter of type std::experimental::dynarray, which represents run-time fixed-size arrays. This type can be created with any size at run time, but cannot change size once created. Lines 16–18 demonstrate passing arrays of various sizes to this function.

Note: std::experimental::dynarray is part of the Arrays Technical Specification, which provides experimental features that may soon be introduced to the C++ standard. It should not be used in production code.


  • Joseph Mansfield

Last Updated

09 April 2015


Fork this sample on GitHub