C++ Templates: The complete Guide in context of C++11

327 views
Skip to first unread message

Alok

unread,
Sep 4, 2011, 5:00:20 AM9/4/11
to
I was just wondering, How far does the book:

C++ Templates: The complete Guide
by David Vandevoorde & Nicolai Josutils,

hold good in context of the the new C++11 standard? What are the
relevant areas which will need an update, or don't hold good anymore
within the modifications that will come through in C++11?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Johannes Schaub

unread,
Sep 4, 2011, 6:36:13 PM9/4/11
to
Alok wrote:

> I was just wondering, How far does the book:
>
> C++ Templates: The complete Guide
> by David Vandevoorde & Nicolai Josutils,
>
> hold good in context of the the new C++11 standard? What are the
> relevant areas which will need an update, or don't hold good anymore
> within the modifications that will come through in C++11?
>
>

One area that definitely needs an update are the relaxed typename rules. In
C++11 you often don't need it anymore:

template<typename T>
struct A {
typedef int type;
A::type x;
};

The declaration "A::type x" is well-formed in C++11 while it's
ill-formed in
C++03. "type" is a member of the current instantiation. However, still
specifying "typename" there won't hurt - it's optional.

In other areas, C++11 is incompatible to C++03. The book makes use of
policies by inheriting from a virtual base class. C++11 makes some of those
uses ill-formed, but I believe I checked the book and didn't find it
affected by these changes (i could have overlooked something...); still
it's
good to know about it:

struct BasePolicy {
typedef int slaptype;
typedef float whiptype;
};

template<typename Pol1>
struct Configurable : virtual BasePolicy, Pol1 {
typename Configurable::whiptype whipkind;
};

Say that the user modifies "whiptype" as follows

struct DoubleWhipper : virtual BasePolicy {
typedef double whiptype;
};

// ill-formed in C++11, well-formed in C++03
Configurable<DoubleWhipper> c;

In C++03 "Configurable::whiptype" is a dependent type and looked up at
instantiation time and will find DoubleWhipper::whiptype unambiguously
using
the dominance rule. But in C++11, "Configurable::whiptype" is a non-
dependent type, but will be looked up twice to check consistency: In the
definition of Configurable, and when it is instantiated. The program
will be
ill-formed if both lookups are different, which is the case in the above
(first lookup will find BasePolicy::whiptype, second lookup by dominance
will find DoubleWhipper::whiptype).

SG

unread,
Sep 4, 2011, 6:36:31 PM9/4/11
to
On 4 Sep., 11:00, Alok wrote:
> I was just wondering, How far does the book:
>
> C++ Templates: The complete Guide
> by David Vandevoorde & Nicolai Josutils,
>
> hold good in context of the the new C++11 standard?

It's still a very good book. Definitely worth reading!

> What are the
> relevant areas which will need an update, or don't hold good anymore
> within the modifications that will come through in C++11?

It's not so much "modifications". Rather "additions". Off the top of
my head:
- variadic templates
- support for default template arguments for function templates
- extended SFINAE (w.r.t. expressions for decltype, sizeof, etc)
- deducing template argument T for the "perfect forwarding pattern"
T&&
- the keyword "typename" will be tolerated in more (even non-template)
contexts

Cheers!
SG

Johannes Schaub

unread,
Sep 4, 2011, 6:37:17 PM9/4/11
to
Alok wrote:

> I was just wondering, How far does the book:
>
> C++ Templates: The complete Guide
> by David Vandevoorde & Nicolai Josutils,
>
> hold good in context of the the new C++11 standard? What are the
> relevant areas which will need an update, or don't hold good anymore
> within the modifications that will come through in C++11?
>

It would be good to extend it with alias templates and the new
enumerations,
including those defined out-of-class:

template<typename T>
struct A {

enum B : int;
};

template<typename T>
enum A<T>::B : int {
X, Y, Z
};

In this case, we introduced members X, Y and Z into class "A<T>" after its
complete body has been seen, which is something completely new in C++11.
This adds some new complications, like what happens if you name "A::X"
before the definition of B has been seen and what happens to a scoped
enumeration's enumerators when the enclosing class is implicitly
instantiated:

template<typename T>
struct A {

enum class B { X = sizeof(T) };
};

A<void> a;

C++11 specifies that instantiation of "X" is not done in this example, such
that this code is well-formed.

Saeed Amrollahi Boyouki

unread,
Sep 5, 2011, 6:21:10 PM9/5/11
to
On Sep 4, 12:00 pm, Alok <aloks...@gmail.com> wrote:
> I was just wondering, How far does the book:
>
> C++ Templates: The complete Guide
> by David Vandevoorde & Nicolai Josutils,
>
> hold good in context of the the new C++11 standard? What are the
> relevant areas which will need an update, or don't hold good anymore
> within the modifications that will come through in C++11?

{ quoted banner removed - please do it yourself. -mod }

Hi
I believe, it's one of the best books written on C++ templates.
I have this book on my table.
In addition to what other wrote, the following
should be added:
- right-angle brackets
- template alias
- Metaprogramming facilities
- tuples
- Local types as template arguments
of course I think, there are a lot of features like move semantics,
generalized
constant expressions, initializers lists, lambdas, auto, ...
which affect on the some parts of book.

Regards,
-- Saeed Amrollahi

Reply all
Reply to author
Forward
0 new messages