Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@rmartinho /arrays.h
Last active

Simple functions for better usage of arrays
// begin() and end() functions for arrays (C++03)
// Written in 2012 by Martinho Fernandes
// To the extent possible under law, the author(s) have dedicated all copyright and related
// and neighboring rights to this software to the public domain worldwide. This software is
// distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along with this software.
// If not, see <>.
// Installation:
// Place this into a header file and #include it in your code. Done.
// Usage:
// int array[10]; // create an array of ten ints
// int* first = so::begin(array); // get a pointer to the beginning
// int* last = so::end(array); // get a pointer to one past the end
// size_t n = so::size(array); // get the size of the array (10)
#include <cstddef> // size_t
namespace so {
template <typename T, std::size_t N>
T* begin(T(&arr)[N]) { return &arr[0]; }
template <typename T, std::size_t N>
T* end(T(&arr)[N]) { return &arr[0] + N; }
template <typename T, std::size_t N>
std::size_t size(T const(&)[N]) { return N; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.