Created
November 16, 2022 08:55
-
-
Save CyberDNIWE/e872a0af13d45107d65bd76463d923cc to your computer and use it in GitHub Desktop.
poorly::sizeof_array, drop-in replacement for plain-c array size deduction during compile time
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
// poorly::sizeof_array is drop-in replacement for plain-c array size deduction | |
// to never have to worry about sizeof(arr)/sizeof(arr[0]) and similar ever again | |
// when needing to pass arrays as ptr and size | |
namespace poorly | |
{ | |
template<typename T, typename SIZE_T = size_t, SIZE_T Size> | |
constexpr SIZE_T sizeof_array(T (&)[Size]) noexcept { return Size; } | |
}; | |
// Test it out! | |
constexpr size_t test() noexcept | |
{ | |
using namespace poorly; | |
int arr[] = { 3, 4, 6, 8, 10, 30, 45, 69 }; | |
return sizeof_array(arr); | |
} | |
constexpr size_t sizeOfMyTestArray = test(); | |
// Will be evaluated to 8(UL) in compile time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment