0

Due to a very very long list of constraints and gotchas... in a special math library that uses libc++ (and in other cases, libstdc++), which I am trying to extend, I am attempting to fully specialize just the division operation because in libc++ it uses functions like scalbn and logb that are not available for the type in question, A in this example. For libstdc++, it doesn't matter since those functions aren't called. Here is a minimal case that triggers the error message I'm running into; see it not work. It is not apparent to me from looking at the -E output what other base template, probably with more parameters, is causing this message, though it also happens with libstdc++. Yes, I realize I should not be defining things in the standard namespace. Yes, I am aware of ADL being able to bring in namespaces as a way to prevent defining things in std, as well as other techniques. That is not what I want to do here. Thank you for any help you can give.

#include <complex>
#include <iostream>

template <class T> struct A{ T a; };

namespace std{
//template <>  // if uncommented, warning: extraneous template parameter list in template specialization
template <class T>
inline std::complex<A<T>>
operator/ <A<T>> (  // either way, error: function template partial specialization is not allowed
 std::complex<A<T>> const&,
 std::complex<A<T>> const&) {
 std::cerr<<"full explicit specialization called\n";
 return std::complex<A<T>>();
}
}

int main(int,char*[]){
 A<double> myA{10};
 A<double> myOtherA{5};
 std::complex<A<double>> r(std::complex(myA)/std::complex(myOtherA));
 std::cerr<<r.real().a<<"\n";
 return 0;
}
Dank
  • 1
  • 1
    Actually, in most cases, [adding specializations to `std` is fine](https://en.cppreference.com/w/cpp/language/extending_std#Adding_template_specializations). What you are doing in particular is UB because instantiating `std::complex` for types other than `float`, `double`, and `long double` isn't allowed, but if that restriction were removed then specialization of `operator/` would be perfectly fine. (Yes, I know that's not the main point of your question, but it might be a useful observation.) – Daniel H Jun 04 '18 at 21:08
  • I considered that a long time ago, but I decided against making the implementation asymmetric with respect to the types that normally use std::complex. This thing interfaces with Eigen, which among many other pieces of functionality tied to std::complex, returns eigenvectors of matrices of type std::complex if the input matrix is type A. – Dank Jun 04 '18 at 21:24
  • Also, in my actual implementation, there are many templates I have added to std and it is hard to keep only the example in mind while writing the description. – Dank Jun 04 '18 at 21:26
  • Hrm, I am not sure if this will work for me in the full version, but I just came up with an idea to use a more constrained base template rather than a template specialization https://wandbox.org/permlink/0Q7qa9ix5k2nAPM7 – Dank Jun 04 '18 at 21:57

0 Answers0