fork(1) download
  1. #include <iostream>
  2. #include <tuple>
  3. using namespace std;
  4.  
  5. template<unsigned TAG_VALUE>
  6. struct tagged_struct {
  7. constexpr static auto S_TAG = TAG_VALUE;
  8. };
  9.  
  10. template<typename Tuple, size_t TAG, size_t I = std::tuple_size<Tuple>::value - 1>
  11. struct tag_to_index {
  12. constexpr static auto value = std::tuple_element<I, Tuple>::type::S_TAG == TAG ? I : tag_to_index<Tuple, TAG, I - 1>::value;
  13. };
  14.  
  15. template<typename Tuple, size_t TAG>
  16. struct tag_to_index<Tuple, TAG, -1> { constexpr static auto value = -1; };
  17.  
  18. int main() {
  19. tuple<tagged_struct<15>, tagged_struct<17>, tagged_struct<29>> structs;
  20.  
  21. cout << tag_to_index<decltype(structs), 17>::value << endl;
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1