Created
November 12, 2012 15:33
-
-
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.
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
#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