Search code examples
c++templatesc++17type-deduction

C++ deduce pointer to member template parameter


I am trying to write a templated version for a lexicographical compare using C++17 features:

#include <type_traits>
#include <set>
#include <vector>
#include <iostream>

namespace compare
{
    template <typename T, auto P, auto ...Ps>
    bool less(const T &t1, const T &t2)
    {
        if constexpr (sizeof...(Ps) == 0)
        {
            return t1.*P < t2.*P;
        }
        else
        {
            if (t1.*P == t2.*P)
            {
                return less<Ps...>(t1, t2);
            }
            else
            {
                return t1.*P < t2.*P;
            }
        }
    }
}

struct my_type
{
    int i = 0;
    std::set<int> s;
};

int main()
{
    my_type t1, t2;

    t2.i = -1;

    std::cout << std::boolalpha << compare::less<my_type, &my_type::i, &my_type::s>(t1, t2) << std::endl;

    return 0;

}

However, my example does not compile with the latest version of GCC:

main.cpp: In instantiation of 'bool compare::less(const T&, const T&) [with T = my_type; auto P = &my_type::i; auto ...Ps = {&my_type::s}]':
main.cpp:42:88:   required from here
main.cpp:16:23: error: no matching function for call to 'less<&my_type::s>(const my_type&, const my_type&)'
     return less<Ps...>(t1, t2);
            ~~~~~~~~~~~^~~~~~~~
main.cpp:6:7: note: candidate: template<class T, auto P, auto ...Ps> bool compare::less(const T&, const T&)
  bool less(const T &t1, const T &t2)
       ^~~~
main.cpp:6:7: note:   template argument deduction/substitution failed:

Interestingly, the compilers output does not state which template parameter could not be deduced. I know how to implement this in the pre C++17 way. What am i doing wrong?


Solution

  • You are missing the T in your less<Ps...>.

    namespace compare
    {
        template <typename T, auto P, auto ...Ps>
        bool less(const T &t1, const T &t2)
        {
            if constexpr (sizeof...(Ps) == 0)
            {
                return t1.*P < t2.*P;
            }
            else
            {
                if (t1.*P == t2.*P)
                {
                    return less<T, Ps...>(t1, t2);
                }
                else
                {
                    return t1.*P < t2.*P;
                }
            }
        }
    }
    

    See it live

    Aside: To fit with the way that the Standard Library defines the concept Compare, I would suggest removing the use of ==, and instead have

    if (return t1.*P < t2.*P)
    {
        return true;
    }
    if (return t2.*P < t1.*P)
    {
        return false;
    }
    return less<T, Ps...>(t1, t2);