Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A question about MSVC's <complex> implementation.

Reply
Thread Tools

A question about MSVC's <complex> implementation.

 
 
Steven Emory
Guest
Posts: n/a
 
      05-16-2012
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
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      05-16-2012
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
 
Reply With Quote
 
 
 
 
Steven Emory
Guest
Posts: n/a
 
      05-16-2012
> 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;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments