Last active
October 12, 2015 02:48
-
-
Save rmartinho/3959946 to your computer and use it in GitHub Desktop.
Simple functions for better usage of arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 <http://creativecommons.org/publicdomain/zero/1.0/>. | |
// 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) | |
#ifndef SO_BEGIN_END_HPP_INCLUDED | |
#define SO_BEGIN_END_HPP_INCLUDED | |
#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; } | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment