Type not inherited in SFINAE for multiple inheritance?

吃可爱长大的小学妹 提交于 2019-12-10 19:48:12

问题


I am using a SFINAE mechanism to deduce a type. Resolve<T>::type is deduced to T if class T doesn't contain yes and it's deduced to MyClass if it contains yes.

class MyClass {};

template<typename>
struct void_ { typedef void check; };

template<typename T, typename = void>
struct Resolve { typedef T type; };

template<typename T>
struct Resolve <T, typename void_<typename T::yes>::check> {
    typedef MyClass type;
};

Now, I have the simple test classes as,

struct B1 { typedef int yes; }; // 1
struct B2 { typedef int yes; }; // 2
struct D1 {};  // 3
struct D2 : B1 {};  // 4
struct D3 : B1, B2 {}; // 5 <----

According to the logic following should be the result for above tests:

  1. Resove<B1>::type = MyClass
  2. Resove<B2>::type = MyClass
  3. Resove<D1>::type = D1
  4. Resove<D2>::type = MyClass
  5. Resove<D3>::type = MyClass or compiler error (due to ambiguity between B1, B2)

Strangely, in test case (5) it doesn't happen so. The result is,

Resolve<D3>::type = D3;

Can anyone explain, what magic is happening specially for multiple inheritance ? Not getting compiler error is a standard compliant behavior ? Here is the demo.


回答1:


Why would you expect a compiler error? You know SFINAE stands for Substitution Failure Is Not An Error right?

When you substitute T by D3 the expression becames ambiguous. Due to SFINAE, this failure is not considered an error and your specialization is simply removed as a candidate. It's all about NOT getting a compiler error.



来源:https://stackoverflow.com/questions/7709703/type-not-inherited-in-sfinae-for-multiple-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!