Hello again!
In this video, we are going to look at miscellaneous template features.
The assert() function, or macro, I think, is inherited from C. It is in the header.
assert() will check its argument at run-time.
If the argument is zero, it will call the library abort() function, which will terminate the program
immediately.
Otherwise, if the arguments is not zero, then the program will continue executing, as normal.
So if 'x' is equal to 42, then nothing will happen.
The program will continue.
If 'x' is not equal to 42, then the program will terminate immediately.
And this is useful for checking invariants.
So these are things that must be true.
For example, if you are going to pass 'x' to a function, and it is very important that the function argument
has the value 42, then this is one way that you can enforce that. There is some runtime overhead to
doing these checks.
And also this is really something that you do not want, in code that is being used by customers. Just
existing with no explanation.
So it is possible to turn this off.
If you define the symbol 'NDEBUG' in the pre-processor, then the assert macro does nothing at all.
In C++11, we have a compile-time version of assert(), called static underscore assert.
This takes a bool argument, and a string.
These have to be available at compile time.
So the bool has to be a constant expression, and the string has to be a string literal. Not a C++ library
string.
The compiler will evaluate this bool.
If the bool evaluates as false, then the compiler will immediately stop processing the source code.
And it will produce an error, which includes the string as part of the error message.
If the bool evaluates to true, then the compiler will carry on, and compile the rest of the source code.
So if the size of int is equal to 8, then nothing will happen.
If size of int is not equal to 8, then the compiler will stop. And the error message will include
the words,
"This program requires a 64-bit compiler"
The main use for this is in template
metaprogramming, again for checking invariants.
If you have a template function, and your code assumes that one of the arguments can be copied, you
can check this, by using static_assert() with a type trait.
So let's quickly try that out. And we get a compiler error.
"This requires a 64-bit compiler"
And in fact, I am compiling in 32-bit mode. And in 32-bit mode, the size of int is 4.
So this boolean is false, and we get this error message when the compiler stops.
We can have template classes which have a default type parameter, or parameters, in the plural.
We write them like this.
So by default, the template parameter is type int.
And then we can just instantiate this, like a normal template class. So we can put long double as the
type parameter, and then this member will be a long double.
If we leave the angle brackets empty, then we gets the default. And this member will be an int.
So here is that class. We have a constructor, to initialize the member. And a function, which will print
out the member. In the main function,
we instantiate this with long double as the type parameter.
So this will be long double.
And then we instantiate again, using the default type parameter of int.
And then we print out the member.
So with the long double, we get 1.99999, and with the default we get 1, because the int has
been truncated. And you might get a compiler warning about that.
In C++11, it is now possible to have a template function with default parameters.
It is the same syntax again.
So by default, "t1" and "t2" will be const reference to int.
And then we just call the template function as normal, and the compiler will deduce the types.
So here we have this function, which will just print out the sum of its arguments.
Then we can have long double and int variables, which we pass to the function.
And then when we call, it the component will deduce it.
And that may not seem very interesting, but in the next video, I will give you an example where this
is actually rather useful.
Okay.
So that is it for this video.
I will see you next time.
But until then, keep coding!