Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Created November 12, 2012 15:33
Show Gist options
  • Save jarrodhroberson/4059998 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/4059998 to your computer and use it in GitHub Desktop.
How to get the length of an array with a template as well as pass in an array to a function to get its length in the function.
#include <cstdint>
#include <stdio.h>
template<typename T, size_t SIZE>
size_t getSize(T (&)[SIZE]) {
return SIZE;
}
typedef std::uint_fast8_t byte;
template <size_t SIZE>
size_t processArray(const byte (&b)[SIZE])
{
return getSize(b);
}
int main(const int argc, const char* argv[])
{
byte a[] = {1,2,3,4,5,6};
printf("%u\n", processArray(a));
byte b[1024];
printf("%u\n", processArray(b));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment