Conditional enable an alternative assignment operator

烈酒焚心 提交于 2019-12-04 07:35:33

You need to make the enable_if dependent on a template parameter. As it is now, it is only dependent on the outer template parameter within the template definition. But if you instantiate a class from the outer template, then your assignment operator template that is instantiated into that class is not dependent on a template parameter anymore because T will have been substituted already.

Just introduce a dummy parameter equal to T

template<typename T1 = T, typename =
    typename std::enable_if<
        ( !std::is_same<
            X<T1>,
            typename StrangerTypeRules<T1>::type
        >::value ),
        X<T1>
    >::type
>
X<T1> & operator=( const typename StrangerTypeRules <T1>::type& rhs ) {
    return *this;
}

Using T1 in just one of theplaces within the enable_if<...> template arguments would suffice already, because that already makes the enable_if dependent.

However it is not the call to your operator= that was previously illformed but the declaration of it, which conflicts with the copy-assignment operator, so the enable_if has little utility here. Just replace your code by

template<typename T1 = T>
X<T1> & operator=( const typename StrangerTypeRules <T1>::type& rhs ) {
    return *this;
}

Since this operator= is a template, it will not conflict with the non-template overload. And also because it is a template, when you call it the compiler will prefer the non-template if T is X<T>.

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