A question about MSVC's <complex> implementation.

Discussion in 'C++' started by Steven Emory, May 16, 2012.

  1. Steven Emory

    Steven Emory Guest

    Hello,

    I've been looking at MSVC's STL implementation ever since watching
    Stefan Lavavej's channel 9 videos to see how things work.

    I was looking at <complex> and wondered why MSVC's complex base class
    inherits from the second template paramenter? In other words, is
    there a particular reason why they do this:

    // forward declarations
    template<class T> class complex;
    template<> class complex<float>;
    template<> class complex<double>;
    template<> class complex<long double>;

    // POS (plain old structures)
    struct fcomplex { float _val[2]; };
    struct dcomplex { double _val[2]; };
    struct lcomplex { long double _val[2]; };

    // base class for explicit specializations
    // why do they inherit from the template argument ValBase?
    template<class T, class ValBase> class complex_base : public ValBase {};

    // explicit specializations
    template<> class complex<float> : public complex_base<float,fcomplex> {};


    instead of:


    // forward declarations
    template<class T> class complex;
    template<> class complex<float>;
    template<> class complex<double>;
    template<> class complex<long double>;

    // base class for explicit specializations
    // doesn't use inheritance but also works
    template<class T>
    class complex_base {
    public :
    T _val[2];
    };

    // explicit specializations
    template<> class complex<float> : public complex_base<float> {};

    Is it just a matter of flexibility perhaps?

    Thanks,
    Steven
     
    Steven Emory, May 16, 2012
    #1
    1. Advertising

  2. On 16.05.2012 09:54, Steven Emory wrote:
    > I was looking at<complex> and wondered why MSVC's complex base class
    > inherits from the second template paramenter? In other words, is
    > there a particular reason why they do this:


    > // base class for explicit specializations
    > // why do they inherit from the template argument ValBase?
    > template<class T, class ValBase> class complex_base : public ValBase {};


    A template base class is a pattern that can be used to add some certain
    functionality to different classes. (E.g. make a container thread safe.)
    In this case most probably they want to be compatible with a C style API
    and the template adds the C++ operator overloading to different bases.


    Marcel
     
    Marcel Müller, May 16, 2012
    #2
    1. Advertising

  3. Steven Emory

    Steven Emory Guest

    > A template base class is a pattern that can be used to add some
    > certain functionality to different classes. (E.g. make a container
    > thread safe.) In this case most probably they want to be compatible
    > with a C style API and the template adds the C++ operator overloading
    > to different bases.
    >
    >
    > Marcel
    >


    Oh, I see why now... thanks Marcel! So it's so that you can do
    something like this:

    typedef struct _fcomplex {
    float _val[2];
    } fcomplex;

    fcomplex obj;
    float (&ref)[2] = reinterpret_cast<float(&)[2]>(obj);
    ref[0] = 1.0f;
    ref[1] = 2.0f;
    std::cout << "(" << ref[0] << ", " << ref[1] << ")" << std::endl;
     
    Steven Emory, May 16, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    588
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    763
    Stanimir Stamenkov
    Oct 25, 2005
  3. Victor Bazarov

    Re: array of complex numbers in MSVC++ 6

    Victor Bazarov, Jul 5, 2003, in forum: C++
    Replies:
    0
    Views:
    485
    Victor Bazarov
    Jul 5, 2003
  4. Robert Mark Bram
    Replies:
    0
    Views:
    700
    Robert Mark Bram
    Feb 4, 2007
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    777
Loading...

Share This Page