vendredi 19 août 2016

C++ template deduction couldn't infer template argument

I have the following scenario:

struct AP;
struct B
{
    B() : m(2) {}
    int m;
};

struct A : private B
{
    A() : B(), n(1) {}
private:
    int n;
    friend AP;
};

struct AP
{
    AP(A& a) : a_(a) {}

    template<typename T>
    struct A_B {
        using type = typename std::enable_if< std::is_base_of< typename std::remove_reference<T>::type,
                                                               A >::value,
                                                                    T >::type;
    };

    template<typename T>
    operator const typename A_B<T>::type&()
    {
        return static_cast<T>(a_);
    }

    template<typename T>
    const typename A_B<T>::type& get()
    {
        return static_cast<T>(a_);
    }

    int& n() { return a_.n; }
private:
    A& a_;
};

int main()
{
    A a;
    AP ap(a);
    ap.n() = 7;
    const B& b = ap.get<const B&>();
    //const B& b = ap; candidate template ignored: couldn't infer template argument 'T'
    //auto b = static_cast<const B&>(ap); candidate template ignored: couldn't infer template argument 'T'
    std::cout<<b.m;
}

The commented lines wouldn't compile. Clang++ notes that "candidate template ignored: couldn't infer template argument 'T'"

Why am I not able to get a reference to A's base with the cast operator? I think the code would look much nicer that way.

Aucun commentaire:

Enregistrer un commentaire