dimanche 27 mars 2016

Access typedefs in CRTP

I have code similar to this one.

It compiles under C++14, if I comment typename T::value_type and uncomment auto in test() method.

However I can not make this works in C++11. Even I use decltype I still get error error: invalid use of incomplete type ‘class B’

How can I access correctly the typedef of T::value_type?

Code:

#include <iostream>

template <class T>
class A{
public:
    typename T::value_type
    // auto
    test() const{
        return self()->test2();
    }

private:
    const T *self() const{
        return static_cast<const T*>(this);
    }

};

class B : public A<B>{
public:
    using value_type = int;

    value_type test2() const{
        return 42;
    }
};

int main(){
    B b;
    std::cout << b.test() << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire