lundi 30 avril 2018

cannot resolve overloaded function based on conversion to type

I'm pretty new to C++ template programming. I'd like to design a function element such as e.g.

  1. element<3, 3, 3, 3, 3> will return 3
  2. element<3, 3, 2> will fail an assertion

    #include <iostream>
    #include <cstdlib>
    
    namespace meta
    {       
      template<typename T>
      constexpr T element(T x)
      {
        return x;
      }
    
      template<typename T, typename... Ts>
      constexpr T element(T x, Ts... xs)
      {
        constexpr T oth = element(xs...);
        static_assert(oth == x, "element Mismatch");
        return x;
      }
    
      template<int... DIMS>
      void get_elements()
      {
        std::cout << "elements " << element(DIMS...);
      }
    }
    
    int main(int argc, char ** argv)
    {
      static constexpr int D1 = 3, D2 = 3;
    
      meta::foo<D1, D2>();
    }
    
    

But GCC with std=c++14 is failing with

cannot resolve overloaded function ‘element’ based on conversion to type ‘int’ E = meta::element<D1, D2>; ^

I'd like to exploit recursion to perform an equality check on each template argument in the list, and return one of them if they're all equal.

Aucun commentaire:

Enregistrer un commentaire