什么是“std :: nullptr_t”模板参数的正确用法? [英] What is a proper use-case of `std::nullptr_t` template parameters?

查看:444
本文介绍了什么是“std :: nullptr_t”模板参数的正确用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我了解到C ++允许类型 std :: nullptr_t 的非类型模板参数:

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?

推荐答案

似乎这是允许避免需要特殊情况模板使用指针类型和 std :: nullptr_t 的值。那样,用例看起来像这样:

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;

也就是说,指针值可以是模板参数,因此 nullptr 也应该。

That is, pointer values can be template arguments and, thus, nullptr should be, too.

这篇关于什么是“std :: nullptr_t”模板参数的正确用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆