How to get element from std::tuple by type

后端 未结 3 419
别跟我提以往
别跟我提以往 2020-12-19 11:54

I have a set of classes A, B, C and I want to have access instances of them from generic code by type, f.e

template
newObject()
{
    retur         


        
3条回答
  •  太阳男子
    2020-12-19 12:26

    You can also compute the position of the type with a constexpr function if you don't like template.

      constexpr int count_first_falses() { return 0; }
    
      template 
      constexpr int count_first_falses(bool b1, B... b)
      {
        if (b1) return 0;
        else return 1 + count_first_falses(b...);
      }
    
      template 
      decltype(auto) tuple_get_by_type(const std::tuple& tuple)    
      {
        return std::get::value)...)>(tuple);
      }
    

提交回复
热议问题