Created
November 15, 2017 02:13
-
-
Save dnaga392/54ccc73f7a309eb2bd1e7b0f85a78081 to your computer and use it in GitHub Desktop.
(C++11) example to get array length by template method.
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 <iostream> | |
template<typename T, std::size_t N> | |
std::size_t array_length(const T (&a)[N]) | |
{ | |
return N; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int array[10]; | |
std::cout << array_length(array) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref.
(JP) http://cpp.aquariuscode.com/array_size_template
(JP) http://www.techscore.com/blog/2013/02/08/how-to-calculate-array-length-in-c-and-cpp/
(JP) http://d.hatena.ne.jp/pknight/20100317/1268796690