今天我开始知道C++允许类型的非类型模板参数std::nullptr_t
:
template<std::nullptr_t N> struct A { };
template<std::nullptr_t N> void f() { }
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我无法想出任何明智的用例.任何人都可以为此提出理由吗?
Die*_*ühl 21
似乎允许使用指针类型和值来避免使用特殊案例模板std::nullptr_t
.那个,用例看起来像这样:
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;
Run Code Online (Sandbox Code Playgroud)
也就是说,指针值可以是模板参数,因此也nullptr
应该是.
我想这在这样的设置中最有用:
template <typename T, T Value> struct Foo;
Foo<int, 10> x;
Foo<std::nullptr_t, nullptr> y;
Run Code Online (Sandbox Code Playgroud)
没有坏处.
(也许std::integral_constant
就是一个例子.)