Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9299053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:28:18+00:00 2026-06-18T22:28:18+00:00

Given an arbitrary numeric type which may or may not be a std::complex type,

  • 0

Given an arbitrary numeric type which may or may not be a std::complex type, I’d like to get the type that represents the “real part” of that type. For example, the real part of std::complex<double> is double, and the real part of double is double itself. The example below uses C++ partial template specialization to accomplish this. @mfontanini has posted an even simpler method below.

My question: is there a direct way of doing this that is already available in the Boost library? If so, I have been unable to find it.

#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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:28:19+00:00Added an answer on June 18, 2026 at 10:28 pm

    There’s no need to use type traits, you can accomplish the same using only template specialization:

    // general case
    template <typename T>
    struct RealPart {
        typedef T type;
    };
    
    // std::complex
    template <typename T>
    struct RealPart<std::complex<T> > {
        typedef T type;
    };
    

    Whether this is already implemented somewhere in boost, I don’t really know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given some arbitrary SQL I would like to get the data types of the
Given four (x,y) pairs that represent the four corners of an arbitrary polygon (quadrilateral)
Given that the web application doesn't have su privileges, I'd like to execute a
Given an arbitrary number of seconds, how can I get the number of years,
Given a non-negative integer n and an arbitrary set of inequalities that are user-defined
I would like to create a function that creates regex matching an arbitrary string
Given an arbitrary array of size n , I'd like to reorganize the elements
I need a counter algortihm which use arbitrary given digits for counting purpose. My
Does a Java security API exists which, given an arbitrary certificate and a list
I would like to determine where the zone is given an arbitrary number. zones

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.