samedi 2 mars 2019

Overloading and template specialization for function call priority

With reference to the following code:

I am trying to conditionally compile a bunch of functions and then 'order' them using the prioirty_tag class. My question is, if I replace enable_if_t<is_nothrow_move_constructible<U>{}>* = nullptr> with enable_if_t<is_nothrow_move_constructible<U>{}>> the output is incorrect (defaults to the first function).

What exactly is happening there? why does adding the * = nullptr make it work?

#include <iostream>
#include <type_traits>

using namespace std;

template <size_t T>
struct priority_tag: priority_tag<T-1> {};
template <>
struct priority_tag<0> {};

template <typename T>
struct my_vec
{   
        template <typename U = T, typename = void>
        void realloc_impl(priority_tag<0> pr)
        {
            cout << "Move throw construct\n";
        };

        //template <typename U = T, enable_if_t<is_copy_constructible<U>{}>> this wont work!

        template <typename U = T, enable_if_t<is_copy_constructible<U>{}>* = nullptr>
        void realloc_impl(priority_tag<1> pr)
        {
            cout << "copy construct \n";
        };

        //template <typename U = T, enable_if_t<is_copy_constructible<U>{}>> this wont work!

        template <typename U = T, enable_if_t<is_nothrow_move_constructible<U>{}>* = nullptr>
            void realloc_impl(priority_tag<2> pr)
        {
            cout << "nothrow move \n";
        };
        void realloc()
        {
                priority_tag<2> pr;
                realloc_impl(pr);
        }

        const static int val = is_nothrow_move_constructible<T>{} ? 1 : is_copy_constructible<T>{} ? 2 : 3;

        priority_tag<val> g;

};

class A {
public:
    A() = default;
    A(A&&) noexcept = default;
};

class B {
public:
    B() = default;
    B(B&&) = delete;
    B(const B&) = default;
};

class C {
public:
    C() = default;
    C(C&&) {}
    C(const C&) = delete;
};


int main()
{
        my_vec<A> obj;
        obj.realloc();

        cout << obj.val;
}

Aucun commentaire:

Enregistrer un commentaire