dimanche 28 février 2016

CRTP compile detection with template composition

I'm stuck with templates problems since few days and you solve each of my problem at a time so thank you in advance.

So I've a template (tl1) who care about a uml composition, and another template (tl2) wich is the uml composed
So my goal is to not compile if the composed object is not a derived of tl2 and if typename D is not a tl1 derived.

Following this post and the help of this one I've got the following code:

#include <type_traits>
#include <list>
#include <string>

template <typename T, typename C>
class tl2 ;

template <typename D, typename T>
class tl1 {
private:
    static_assert(std::is_base_of<tl2<T, D>, T>::value, "T should     inherit from tl2");
    std::list<T> mTs ;
    tl1() {} ;
    friend D ;
public:
    T & getTbyName() const ;
};

template <typename T, typename C>
class tl2 {
    //static_assert(std::is_base_of<tl1<C, T>, C>::value, "D should inherit from Database");
public:
    std::string getName() { return mName ; }
private:
    C & mC ;
    std::string mName ;
};

class cl1 ;

class cl2 : public tl2<cl2, int>  {

};
class cl1 : public tl1<int, cl2>  {

};

My problem is this compile very well and I would like not.
It doesn't compile if I change cl1 to:

class cl1 : public tl1<int, cl2>  {    
    cl1() {}
};

The fact is tl1 and tl2 will be in library, so I want to perform all checks in the library. I will not have control over derived so I'd like to be sure that implementation is tlX derived.

Thank you for your time again.
Quentin

Aucun commentaire:

Enregistrer un commentaire