Modern C++ Design

14 views
Skip to first unread message

user name

unread,
Apr 28, 2001, 6:57:02 AM4/28/01
to
I recently got the book Modern C++ Design.

I have also been following some of Andrei Alexandrescu's work
online and have found it very interesting.

Unfortunately I don't have the book with me at this
exact moment, but, I am pretty sure my question centers
around something discussed in the first or second chapter.

I am confused about something Andrei said in the book.

Early in the book he discusses polymorphic approach
versus a template aproach, and then goes on to describe things
templates can do, and then finishes the chapter discussing things
they can't do.

While describig partial specialization and its limitations I came
across text that (I recall) said something like:

Partial specializations cannot structurally alter the class,
and there was a reference to changing its members.

I had always assumed that the following was legal:

struct A {};
struct B {};
struct C {};

template <typename T, int I> struct X
{
int i;
A a;
}; // primary template

template <typename T> struct X<T, 10>
{
float f;
double d;
B b;
}; // specialization for I == 10

int main () {}

Now, compilers seem to like this code,
and it looks like I am structurally altering the class
for a set of specializations.

Maybe I misunderstand what he meant in which case
I would like to know what what limitation he had in mind
specifically, orr perhaps the code above is ill-formed and
I would like to see the relevant sections that describe it
as such.

I am really sorry that I don't have the exact text
I am reacting to, but if people are confused
by it, and can not find the section I am refering to
I will repost with the exact page and line.

(If you have no noticed, I am firing out a bunch of
posts at one sitting, and don't want to delay my question
any longer.)

------------------------------------------------------------
Get your FREE web-based e-mail and newsgroup access at:
http://MailAndNews.com

Create a new mailbox, or access your existing IMAP4 or
POP3 mailbox from anywhere with just a web browser.
------------------------------------------------------------

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]

Andrei Alexandrescu

unread,
Apr 28, 2001, 7:35:39 PM4/28/01
to
"user name" <use...@MailAndNews.com> wrote in message
news:3AF8...@MailAndNews.com...
[snip]

> Partial specializations cannot structurally alter the class,
> and there was a reference to changing its members.
[snip]

Your code is correct. Note that in your code you'd unfortunately have to
duplicate the template class's member functions... However, can you
customize part of a class' structure without rewriting its behavior? For
example, can you specialize a class so that you keep the class just as it
is, but change two members from it from int to double?

You can, but not through specialization alone. That was the point of the
section - you need to combine a couple of techniques to achieve a flexible
class design.


Andrei

Reply all
Reply to author
Forward
0 new messages