Created
July 17, 2025 04:32
-
-
Save ParadoxV5/bae9c392b3e46fe8c991078640a91387 to your computer and use it in GitHub Desktop.
C++ Compile-time Fixed-Size Array of Types
This file contains hidden or 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 <tuple> | |
| #include <type_traits> | |
| #include <iostream> | |
| using std::size_t; | |
| /** Compile-time Fixed-Size Array of Types */ | |
| template<typename... T> struct TypeArray { | |
| using TupleType = std::tuple<T...>; | |
| template<size_t i> using At = | |
| typename std::tuple_element<i, TupleType>::type; | |
| }; | |
| /** Example array */ | |
| using MyArray = TypeArray<float, const uint64_t, std::string>; | |
| /** Example fetch */ | |
| using TypeFromArray = MyArray::At<1>; | |
| /** Verify */ | |
| static constexpr bool same = std::is_same<TypeFromArray, const uint64_t>::value; | |
| int main() { | |
| if(!same) | |
| std::cout << "not "; | |
| std::cout << "same" << std::endl; | |
| return !same; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment