开发者

Template definition order problem

I've got a simple mixin, which I am mixing in to my other template classes.

template<typename T> class mixin {
    static T* null() { return nullptr; }
    auto func() -> decltype(null()-开发者_开发百科>func());
};
template<...> class A : public mixin<A<...>> {
    ....
};
template<...> class B : public mixin<A<...>> {
    ....
};
template<...> class C : public mixin<A<...>> {
    ....
};

Now, I've got a problem. One of the mixin functions will return a type which must be deduced depending on the derived type. But when I attempt to use deduction to find this type, the compiler tells me that I am using an undefined type. If I move the definition of mixin to be after the classes, then I won't be able to inherit from it when mixing in. How can I change my classes to allow type deduction in this case?


I don't believe there's any way to make this work. You have a cyclic dependency between the types of each class. A needs the definition of mixin<A<...>> and mixin<A<...>> needs the definition of A.

In my opinion, you would be best just to manually specify the type in the mixin parameters.

For example:

template<typename ReturnType> class mixin
{
    auto func() -> ReturnType;
};

template<...> class A : public mixin<int>
{
    int func();
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜