Условно включить альтернативный оператор присваивания
Я пытаюсь условно создать экземпляр дополнительного оператора присваивания. Код ниже хорошо работает в Clang, но не в GCC 4.7.
Проблема, которую я имею, кажется очень похожей на вопрос, заданный здесь:std :: enable_if для условной компиляции функции-члена
Следующее иллюстрирует проблему, которую я имею:
#include <type_traits>
template<typename T>
struct StrangerTypeRules;
template<typename T>
struct X;
template< >
struct StrangerTypeRules < unsigned > {
typedef unsigned type;
};
template< >
struct StrangerTypeRules < bool > {
typedef X<bool> type;
};
template<typename T>
struct X {
// In the non-trivial version of my code, I can not use the
// default assignment operator, therefor I need to define this one
X& operator=( const X<T>& rhs ) {
return *this;
}
// Alternative assignment oprtator, must only exists if it is
// different from the assignment operator above
template<typename =
typename std::enable_if<
( !std::is_same<
X<T>,
typename StrangerTypeRules<T>::type
>::value ),
X<T>
>::type
>
X<T> & operator=( const typename StrangerTypeRules <T>::type& rhs ) {
return *this;
}
};
int main(int argc, const char *argv[])
{
X<unsigned> x1, x2;
x1 = 4;
x2 = x1;
X<bool> x3, x4; // compile error in gcc 4.7 with -std=c++11
//x3 = x4;
return 0;
}
Можно ли это сделать способом, который удовлетворяет как clang, так и gcc 4.7? Если так, то как?
Ошибка компиляции при использовании gcc:
test.cxx: In instantiation of ‘struct X<bool>’:
test.cxx:52:13: required from here
test.cxx:38:12: error: no type named ‘type’ in ‘struct std::enable_if<false, X<bool> >’