I use #include <type_traits> I don’t know such work with templates, but I would like to understand Anywhere there is good material on the introduction of such "management" of templates?

I would like to understand how to specialize templates for a pointer to a class, for a pointer to simple types, etc.

In this code, the error is: error C2593: неоднозначный "operator &" He cannot choose between the operators that he marked ### in the comment.

 #include <type_traits> struct A { // для массивов // Для массивов указано правильно или можно сделать лучше? template <typename T, size_t N, size_t M> void operator&(T (& obj)[N][M]){} // ### template <typename T, size_t N> void operator&(T (& obj)[N]){} // для классов template<typename T> typename std::enable_if<!std::is_enum<T>::value,void>::type operator&(T & obj){obj.serialize(*this,0);} // для указателя на класс template<typename T> typename std::enable_if<std::is_class<T>::value,void>::type operator&(T * obj){ if(obj != 0){obj->serialize(*this,0);}} // ### для указателя на простые типы template<typename T> typename std::enable_if<!std::is_class<T>::value && !std::is_array<T>::value && !std::is_enum<T>::value,void>::type operator&(T * obj){ if(obj != 0){}} }; int main() { A a; int * p_int; a& p_int; unsigned long long maps[200000]; a&maps; std::system("pause"); return 0; } 
  • And why do you overload the unary operator, and use binary in the code? - dzhioev
  • And how to operator & use unary? - manking

1 answer 1

Later update : your check is not quite correct. The array will pass your check to the pointer, since you pass it to operator& as T* (which means it can convert). To disable the conversion, you can either rewrite the function like this:

 template<typename T> typename std::enable_if< !std::is_class<T>::value && !std::is_array<T>::value && !std::is_enum<T>::value, void>::type operator&(T*& obj) { if (obj != 0) { /**/ } } 

(this will work, but only for lvalue pointers), either:

 template<typename T> typename std::enable_if< std::is_pointer<T>::value && !std::is_class<typename std::remove_pointer<T>::type>::value && !std::is_array<typename std::remove_pointer<T>::type>::value && !std::is_enum<typename std::remove_pointer<T>::type>::value, void>::type operator&(T obj) { if (obj != 0) { /**/ } } 

Old answer:

You went the wrong way: partial specialization of template functions does not exist , what you are doing is overloading ! Try a better specialization of template classes.

You get two competing overloads, it is clear that you have ambiguity.

Maybe this option will suit you:

 #include <iostream> #include <type_traits> using namespace std; template<typename T> struct A; template <typename T, size_t N> struct A<T[N]> { void operator & (T obj[N]) { cout << "array" << endl; } }; template<typename T> struct A<T*> { void operator & (T* obj) { cout << "pointer" << endl; } }; #define SERIALIZE(X) { A<decltype(X)> _a; _a & X; } int main() { unsigned long long maps[2000]; SERIALIZE(maps); SERIALIZE(&maps[0]); return 0; } 

Issues:

array
pointer