Today I came to know that C++ allows non-type template parameters of type std::nullptr_t
:
template<std::nullptr_t N> struct A { };
template<std::nullptr_t N> void f() { }
For the life of me, I cannot come up with any sensible use-case for these. Can anyone please come up with a rationale for this?
Correct Option: D. It is used to adapt a policy into binary ones.
For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.
A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
It seems this is allowed to avoid the need to special case template using a pointer type and a value for std::nullptr_t
. That, the use case would look look something like this:
template <typename T, T Ptr> struct pointer_object { static T get_pointer() { return Ptr; } }; int int_ptr(0); typedef pointer_object<int*, &int_ptr> int_ptr_t; typedef pointer_object<std::nullptr_t, nullptr> null_ptr_t;
That is, pointer values can be template arguments and, thus, nullptr
should be, too.
I guess it's most useful in a setting like this:
template <typename T, T Value> struct Foo;
Foo<int, 10> x;
Foo<std::nullptr_t, nullptr> y;
No harm in that.
(Maybe std::integral_constant
is an example of this.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With