Good day. There are two operator overloads for my class, the correspondence between the types of parameters and those for which the operator is supported is checked using an additional type in the template and enable_if , and the template header is the same for two functions:
#define TEMPLATE_T1_T2_WITH_PARAM_TYPE_CHECK template<typename T1, typename T2,\ class = typename std::enable_if<\ (std::is_same<T1, dstring>::value ||\ std::is_same<typename std::remove_extent<T1>::type, char>::value &&\ std::is_array<T1>::value) &&\ (std::is_same<T2, dstring>::value ||\ std::is_same<typename std::remove_extent<T2>::type, char>::value &&\ std::is_array<T2>::value)\ >::type> TEMPLATE_T1_T2_WITH_PARAM_TYPE_CHECK friend dstring operator+(const T1& lhs, const T2& rhs){ dstring res(lhs); res += rhs; return res; } TEMPLATE_T1_T2_WITH_PARAM_TYPE_CHECK friend bool operator!=(const T1& lhs, const T2& rhs){ return !(lhs == rhs); } The question is: is the declaration of the template header a macro an adequate and acceptable way out in this situation, or will it be better to duplicate the code? Still, many do not advise using macros, and in this case it could lead to problems with debugging.
P.S. And whether operator overload for different combinations of several types is acceptable as a template, or is it better to create a regular overload for each case with the same body, like dstring + dstring , dstring + char* , char* + dstring , etc. ?
dstringtypes, an instance of each function will be created. Maybe think aboutoperator dstring()? - user58697