型である場合もそうでない場合もある任意の数値型が与えられた場合、std::complex
その型の「実数部」を表す型を取得したいと思います。たとえば、の実数部std::complex<double>
はdouble
であり、の実数部double
はdouble
それ自体です。以下の例では、C++の部分テンプレート特殊化を使用してこれを実現しています。@mfontaniniは、さらに簡単な方法を以下に投稿しています。
私の質問:Boostライブラリですでに利用可能なこれを行う直接的な方法はありますか?もしそうなら、私はそれを見つけることができませんでした。
#include <complex>
#include <boost/type_traits/is_complex.hpp>
template <typename T>
class RealPart
{
private:
template <bool, typename>
class ResultType;
// complex type -> real type
template <typename T1>
class ResultType<true, T1>
{
public:
typedef typename T1::value_type type;
};
// real type -> real type
template <typename T1>
class ResultType<false, T1>
{
public:
typedef T1 type;
};
public:
// define result_type, making use of the template specialization above
typedef typename ResultType<boost::is_complex<T>::value, T>::type result_type;
};
// both will become doubles
RealPart<std::complex<double> > a;
RealPart<double> b;