Reddit reviews C++ Templates: The Complete Guide
We found 13 Reddit comments about C++ Templates: The Complete Guide. Here are the top ones, ranked by their Reddit score.
NewMint ConditionDispatch same day for order received before 12 noonGuaranteed packagingNo quibbles returns
You have a long journey ahead of you, but here goes :D
Beginner
C++ Primer: One of the better introductory books.
The C++ Standard Template Library: A Tutorial and Reference: Goes over the standard template library in fantastic detail, a must if you're going to be spending a lot of time writing C++.
The C++ Programming Language: Now that you have a good idea of how C++ is used, it's time to go over it again. TCPPL is written by the language's creator and is intended as an introductory book for experienced programmers. That said I think it's best read once you're already comfortable with the language so that you can full appreciate his nuggets of wisdom.
Intermediate
Modern C++ Design: Covers how to write reusable C++ code and common design patterns. You can definitely have started game programming by the time you read this book, however it's definitely something you should have on your reading list.
C++ Templates: Touches on some similar material as Modern C++ Design, but will help you get to grips with C++ Template programming and how to write reusable code.
Effective C++: Practical advise about C++ do's and dont's. Again, this isn't mandatory knowledge for gamedev, but it's advice is definitely invaluable.
Design Patterns: Teaches you commonly used design patterns. Especially useful if you're working as part of a team as it gives you a common set of names for design patterns.
Advanced
C++ Concurrency in Action: Don't be put off by the fact I've put this as an "advanced" topic, it's more that you will get more benefit out of knowing the other subjects first. Concurrency in C++11 is pretty easy and this book is a fantastic guide for learning how its done.
Graphics Programming
OpenGL: A surprisingly well written specification in that it's pretty easy to understand! While it's probably not the best resource for learning OpenGL, it's definitely worth looking at. [edit: Mix it in with Open.gl and arcsynthesis's tutorials for practical examples and you're off to a good start!]
OpenGL Superbible: The OpenGL superbible is one of the best ways to learn modern OpenGL. Sadly this isn't saying much, in fact the only other book appears to be the "Orange Book", however my sources indicate that is terrible. So you're just going to have suck it up and learn from the OGL Superbible![edit: in retrospect, just stick to free tutorials I've linked above. You'll learn more from them, and be less confused by what is 3rd party code supplied by the book. Substitute the "rendering" techniques you would learn from a 3d book with a good 3d math book and realtime rendering (links below)]Essential Mathematics for Game Programmers or 3D Math Primer for Graphics and Game Development: 3D programming involves a lot of math, these books cover topics that OpenGL/DirectX books tend to rush over.
Realtime Rendering: A graphics library independent explanation of a number of modern graphical techniques, very useful with teaching you inventive ways to use your newly found 3d graphical talents!
Actually a more up to date book is C++ Template Metaprogramming by David Abrahams of Boost fame and Aleksey Gurtovoy. It uses the MPL extensively which implements quit a bit of the article if not all. It also describes FC++, Blitz++ and a few others. It implements a basic expression template system and even has an appendix on the Boost Preprocessor library.
I don't recommend starting out with that one though.
Also a good book, is C++ Templates . It's a little bit easier to start off with.
It's a form of metaprogramming — writing programs that run at compile time rather than runtime. Metaprogramming in C++ is a little strange at first because operations don't primarily occur on values, but on types. A metafunction might take a type as an input and generate a result type as an output, for instance. The type system of the language is the execution environment for metaprograms, and hence templates factor into it heavily. A template type parameter can stand for any type, which makes it sort of the equivalent of a variable in metaprogramming.
SFINAE is a very powerful tool used extensively in C++ template metaprogramming. It stands for Substitution Failure Is Not An Error. (The term was coined by the authors of the book C++ Templates: The Complete Guide. It's not an official term in the standard, although the standard of course mandates the behavior that the acronym is describing.) To understand what that means, you need to understand how overload resolution works.
In very broad terms, whenever you call a function, there might be multiple functions with that name. This includes very straightforward overloading:
void f(int x)
{
// ...
}
void f(long x)
{
// ...
}
But it can also include more complicated things, such as:
template <typename T>
void g(T x)
{
// ...
}
void g(double x)
{
// ...
}
The first
g()
is a template function that can be instantiated with any type forT
. So what happens when you callg(3.5)
? The standard lays out a series of steps that happen, called overload resolution.The first thing that happens is to build a list of candidate functions. This can be trickier than you might imagine, because of things like implicit conversions. If there's a function with signature
void f(Widget w)
and I callf(42)
, there might be an implicit conversion that allows constructing aWidget
from anint
, for example. So the compiler chugs through all possible functions, using criteria like the number of arguments (which is of course affected by things like default arguments and variadic functions), and the types of arguments to arrive at a list of viable functions.It's entirely possible that there is more than one viable function. In the
g(3.5)
example, both are viable. The standard specifies a series of criteria used to rank viable functions. One function can be more viable than another, for example if calling it involves fewer implicit conversions, and a bunch of other details like that. (In the example above,void g(double)
is more viable than the template function, because it's an exact match for the argument3.5
.) Anyway, there is a long and complicated series of things that are considered but in the end a most viable function is chosen.Finally, if the most viable function is a template function, then the template arguments are deduced, and the deduced values are substituted into the declaration. For example, if I called
g(12)
, thenT
would be deduced asint
.int
will be substituted forT
in the declaration, formingvoid g(int x)
. That's legal, so everything is fine. The template function is instantiated (T
is substituted in the definition) and called, and everything is fine.But what if the result of that substitution of
T
into the function's declaration results in something that's invalid? Consider:struct Foo {
typedef int fonzie;
...
}
template <typename T>
typename T::fonzie h(T x);
In order for this declaration to be valid,
T::fonzie
has to be a valid expression. (typename
is necessary here to tell the compiler thatfonzie
is a member type. See this page for an explanation.) Clearly if I try to pass an instance ofFoo
, that will be the case, but not otherwise. For example, if I try to callh(42)
, the typeint::fonzie
is nonsense.That's the "substitution failure" part of SFINAE. The "is not an error" part means that this does not halt compilation. Rather, this overload is merely removed from the list of viable functions and the next best match is tried. (If there is no next best match, then that really is an error.)
We can harness this, by intentionally causing substitution failures. That allows us to say "don't consider this for overload resolution", which is in effect a way of steering things, of specifying some logic which is executed at compile time that decides which function is called based on certain criteria.
Here's how
std::enable_if
is implemented. Start with a template class:template<bool B, typename T = void>
struct enable_if {};
This has two template parameters. The first,
B
is a non-type parameter, whereas the second one,T
is a type parameter. It has a default value ofvoid
. Now we provide a partial specialization:template<typename T>
struct enable_if<true, T> { typedef T type; };
If I try to instantiate
enable_if
with the first template parameter equal totrue
, then the result is the above specialization, which is a class that defines a single member type namedtype
. Otherwise, we get the non-specialized class above which is just the empty class. Is it beginning to click?enable_if&lt;true, T&gt;::type
evaluates toT
.enable_if&lt;false, T&gt;::type
is a substitution failure, because there is no such member namedtype
. If this is used as part of the declaration of a function, we can cause that function to not participate in overload resolution based on some true or false condition.This is meant to be combined with other metafunctions, such as type traits. For instance,
std::is_integral&lt;T&gt;::value
is a static member that'strue
ifT
is an integer type (char
,short
,int
,long
,long long
, etc.), otherwise it'sfalse
.template <typename T>
typename std::enable_if<std::is_integral<T>::value, void>::type func(T x);
If
func()
is called with an integral type, thenstd::is_integral&lt;T&gt;::value
istrue
, which means we have:template <typename T>
typename std::enable_if<true, void>::type func(T x);
We said earlier that if the first parameter to
enable_if
is true, then the second parameter is passed through to the member typedef::type
, so this is equivalent totemplate <typename T>
void func(T x);
This is a plain old function returning
void
as long asT
is deduced as an integer type. If it's not, then this is a substitution failure and this function becomes non-viable and does not participate in overload resolution. We could supply another overload that would apply in that case; it could either apply universally, or it could have conditions of its own, such as only accepting types that meet some other condition. This is the equivalent of an if/else or if/else if — we've written a meta-program that says "if the type is such and such, call this, otherwise call this."I hope it's obvious that this is extremely powerful. It's used extensively in the implementation of the standard library, as well as numerous third party libraries like Boost.
If you want to learn more about C++ template metaprogramming, there was recently a talk at CppCon by Walter E. Brown titled Modern Template Metaprogramming - A Compendium. Here's part I, here's part II, and here are the slides (PDF). You can also read the book mentioned above, which is the definitive reference for this stuff, although it has not yet been updated to cover the new goodies in C++11 and C++14 such as variadic templates.
Basic:
C++ Primer
Effective C++
Effective STL
Advanced:
More Effective C++
C++ Coding Standards: 101 Rules
C++ Templates The Complete Guide
Large Scale C++ Design
C++ Templates: The Complete Guide is recommended for deepening your knowledge about templates.
Even if some C++ implementation had 100% correct and perfect support for C++ templates as described in the standard they'd still be horrible and restrictive things.
Go read something like, say: http://www.amazon.com/Templates-Complete-Guide-David-Vandevoorde/dp/0201734842 ... You see; the book is good because it does an excellent job at describing how mind-bogglingly stupid this crap really is for what it tries to do. Every other sentence ends like this; "but this might be fixed or improved in a future version of the language".
No really; there are much better options out there than C++ templates for when one need to generate code at compile-time.
Sorry, it was a lighthearted comment - I just read it in this book yesterday and thought I'd share :-D
If I understand correctly, you're correct, as far as the programmer is concerned, it will operate much like a header file. The tooling and compiler, however, won't have to do text inclusion on EVERYTHING.
I don't want to say anything I'd regret, my standard-ese isn't great, but it does look like, once everything is put together, you'd just replace #include <stdio.h> with import std.io, get intellisense that doesn't take 14 hours to read all your headers, eat up 8gb of RAM, and run your hard drive at 100% :-)
http://www.amazon.com/C-Templates-The-Complete-Guide/dp/0201734842
Another good book, starts a little bit easier on the newbie than Alexandrescu. Between the two of them you should be a template wizard in no time... :)
There were rumors at CppCon2015 that David and Nicolai would be updating their templates book. But, to my knowledge, no official announcement has been made.
First note that Career/Job/Market is quite different from Knowledge/Intellectual satisfaction. So you have to keep "earning money" separate from "gaining knowledge" but do both parallely. If you are one of the lucky few who has both aligned in a particular job, you have got it made. Mostly that is never the case and hence you have to work on your Motivation/Enthusiasm and keep hammering away at the difficult subjects. There are no shortcuts :-)
I prefer Books to the Internet for study since they are more coherent and less distracting, allowing you to focus better on a subject. Unless newer editions are reqd. buy used/older editions to save money and build a large library. So here is a selection from my library (in no particular order);
Umm...?
I'd add C++ Templates: The Complete Guide to the list of essential reading for advanced C++.