A question about MSVC's <complex> implementation.

S

Steven Emory

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
 
M

Marcel Müller

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
 
S

Steven Emory

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top