0

In this code:

// decomplexify ---------------------------------------------------------------
template <typename T>
struct decomplexify
{
  typedef T type;
};

template <typename ELT>
struct decomplexify<std::complex<ELT> >
{
  typedef ELT type;
};

It appears the partial specialization will work for

decomplexify<std::complex<T>>,

but not

decomplexify<std::complex<T>&>

This is on gcc (GCC) 4.9.2 20141101 (Red Hat 4.9.2-1)

Does this seem expected behavior? Is there a workaround (other than a redundant specialization for std::complex&?)

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
nbecker
  • 1,645
  • 5
  • 17
  • 23

1 Answers1

2

Yes, this behaviour is expected. Here's a possible workaround:

template <typename T>
struct decomplexify_impl
{
  typedef T type;
};

template <typename ELT>
struct decomplexify_impl<std::complex<ELT> >
{
  typedef ELT type;
};

#include <type_traits>

template<typename T>
struct decomplexify
{
  typedef typename std::remove_reference<T>::type TT;
  typedef typename decomplexify_impl<TT>::type type;
};

If you don't intent to use the class with types other than std::complex specializations, I suggest you leave the primary template undefined, so that the compiler catches it and errors out.

There's also value_type member typedef of std::complex<T> that gives you T (maybe that's all you need).

jrok
  • 54,456
  • 9
  • 109
  • 141
  • very nice, thanks! In this case the resulting type would have the reference removed, which I think is what I want. But just for curiosity, is there a solution that would preserve reference? `decomplexify&>::type == double&` ? – nbecker Jan 09 '15 at 13:45