Created
December 23, 2023 03:49
-
-
Save SaitoAtsushi/248300ea5660ede601a4d978f112b0de to your computer and use it in GitHub Desktop.
型に連番を割り当てる
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 <cstddef> | |
#include <iostream> | |
#include <type_traits> | |
// 型に結び付いた連番を生成するクラステンプレート | |
template <class... T> | |
class identify_number_traits { | |
private: | |
template <class U> | |
struct foo { | |
template <std::size_t m, class V, class... W> | |
class bar : public std::integral_constant<std::size_t, bar<m + 1, W...>::value> {}; | |
template <std::size_t m, class... W> | |
class bar<m, U, W...> : public std::integral_constant<std::size_t, m> {}; | |
}; | |
public: | |
template <class U> | |
class type : public std::integral_constant<std::size_t, foo<U>::template bar<0, T...>::value> {} | |
}; | |
// なんやかんやの適当なクラス | |
class banana {}; | |
class apple {}; | |
class cherry {}; | |
// 適当なクラスを連番に対応付ける | |
template <class T> | |
using fruits_identify = identify_number_traits<banana, apple, cherry>::type<T>; | |
int main() { | |
std::cout << fruits_identify<banana>::value << std::endl | |
<< fruits_identify<apple>::value << std::endl | |
<< fruits_identify<cherry>::value << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment