本文介绍了是nullptr_t默认可构造类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果nullptr_t有一个默认的构造函数,我不能从C ++ 11标准。换句话说,以下是有效的:

  
  • 价值初始化( nullptr_t n = nullptr_t(); ),它将n初始化为0。



  • int ,因此 nullptr_t 绝对是默认构造的。
    这里有趣的问题是: nullptr_t 有没有定义的值是什么意思?在一天结束时, nullptr_t 只有一个有意义的可能值,它是 nullptr 。此外,类型本身只通过 nullptr 文字的语义来定义。这些语义是否仍然适用于单位化值?



    为什么这个问题在实践中不重要



    不想声明一个类型为 nullptr_t 的新变量。该类型的唯一有意义的语义已经通过 nullptr 字面量表示,所以每当你使用你的自定义变量 nullptr_t ,你可以使用 nullptr 。



    在实践中很重要



    唯一的例外情况是你可以获取类型 nullptr_t 的非类型模板参数。对于这种情况,知道哪些值可以转换为 nullptr_t ,这在4.10中描述是有用的:

    这基本上只是你期望的:你可以写

      nullptr_t n = 0; //正确:0是特殊的

    但不是

      nullptr_t n = 42; // WRONG无法将int转换为nullptr_t 

    gcc 4.6和Clang SVN都可以正确使用。 / p>

    I can't tell from the C++11 Standard if nullptr_t has a default constructor. In other words, is the following valid?:

    nullptr_t n;
    

    GCC and VC++ allow the above code, but clang does not. I can't find anything in the Standard specifying that it doesn't have a default constructor, and what I can find suggests that it ought to. This matters to me because I'm writing a basic fallback implementation of nullptr for older compiler support and need to know if I need to give it a default constructor.

    解决方案

    What the Standard says

    The standard says (18.2)

    Where 3.9.1 basically says it should be of the same size as void* and 4.10 specifies the conversion rules for nullptr.

    Edit: 3.9.9 furthermore explicitly states that nullptr_t is a scalar type, which means the expected initialization rules for built-in types from 8.5 apply:

    • Default-initialization (nullptr_t n;), which leaves the value of n undefined. As Johannes Schaub pointed out correctly, this compiles fine with the newest version of Clang.
    • Value-initialization (nullptr_t n = nullptr_t();), which initializes n to 0.

    This behavior is identical to e.g. int, so nullptr_t is definitely default-constructible.The interesting question here is: What does it mean for nullptr_t to have undefined value? At the end of the day, there is only one meaningful possible value for nullptr_t, which is nullptr. Furthermore the type itself is only defined through the semantics of the nullptrliteral. Do these semantics still apply for an unitialized value?

    Why that question doesn't matter in practice

    You don't want to declare a new variable of type nullptr_t. The only meaningful semantic of that type is already expressed through the nullptr literal, so whenever you would use your custom variable of type nullptr_t, you can just as well use nullptr.

    What does matter in practice

    The only exception to this comes from the fact that you can take non-type template parameters of type nullptr_t. For this case, it is useful to know which values can convert to nullptr_t, which is described in 4.10:

    Which basically does just what you'd expect: You can write

    nullptr_t n = 0;    // correct: 0 is special
    

    but not

    nullptr_t n = 42;   // WRONG can't convert int to nullptr_t
    

    Both gcc 4.6 and Clang SVN get this right.

    这篇关于是nullptr_t默认可构造类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-23 06:37
    查看更多