标签: nullptr

实现安全nullptr

我想保持我的代码可以在遗留C++(使用"NULL"的C++代码)和新的C++ 11标准(使用"nullptr"的C++代码)上编译

我正在使用GCC,但是当我完成最重要的事情时,我还计划为VS重新编译整个代码库.

我是否应该期待GCC和VS都会这样做

#define NULL nullptr
Run Code Online (Sandbox Code Playgroud)

或者我自己会做得更好(当然使用不同的名称,其中MY_LIB将被我的库后缀替换)?

#ifndef nullptr
    #define MY_LIB_NULL NULL
#else
    #define MY_LIB_NULL nullptr
#endif
Run Code Online (Sandbox Code Playgroud)

我想要实现的是编译的代码,无论C++ 11的功能是否已经实现(并且因为我没有使用模板,所以它们很少).

例如,关键字"覆盖"和"最终"已经完成.

MY_LIB_OVERRIDE //macro, defines to "override" if c++11 is present.
MY_LIB_FINAL    //macro, defines to "final" if c++11 is present.
Run Code Online (Sandbox Code Playgroud)

我问这个问题是因为我知道"nullptr"问题有点奇怪,所以也许只是我已经为覆盖和最终做了同样的事情,这是错误的.需要对此提出意见.任何帮助都很好.

c++ overriding final nullptr c++11

6
推荐指数
1
解决办法
1513
查看次数

在c ++ 11中,dynamic_cast是返回nullptr还是0?

我想查看dynamic_cast的结果.在c ++ 11(或c ++ 0x,对于支持nullptr的编译器),我应该与nullptr或0进行比较吗?

这有关系,如果是的话,为什么?

结果是编译器依赖的吗?

dynamic-cast nullptr c++11

6
推荐指数
2
解决办法
5533
查看次数

检查std :: function是否已分配给nullptr

我想知道是否有任何方法来检查你分配到一个函数指针是否std::function是一个nullptr.我期待!-operator能够做到这一点,但它似乎只在函数被分配了类型的东西时起作用nullptr_t.

typedef int (* initModuleProc)(int);

initModuleProc pProc = nullptr;
std::function<int (int)> m_pInit;

m_pInit = pProc;
std::cout << !pProc << std::endl;   // True
std::cout << !m_pInit << std::endl; // False, even though it's clearly assigned a nullptr
m_pInit = nullptr;
std::cout << !m_pInit << std::endl; // True
Run Code Online (Sandbox Code Playgroud)

我写了这个辅助函数来解决这个问题.

template<typename T>
void AssignToFunction(std::function<T> &func, T* value)
{
    if (value == nullptr)
    {
        func = nullptr;
    }
    else
    {
        func = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ std nullptr c++11 std-function

6
推荐指数
1
解决办法
4154
查看次数

'nullptr'之前的预期unqualified-id

我试图实施BST,但std::nullptr向我显示错误:

错误:'nullptr'之前的预期unqualified-id

#include <iostream>
#include <memory>

template <typename T>
class BinTreeNode {
public: 
    BinTreeNode(T key): data {key} {
        left = std::nullptr;
        right = std::nullptr; 
    }
    ~BinTreeNode() {}
    T data;
    BinTreeNode<T>* left;
    BinTreeNode<T>* right;
};

template <typename T>
class BinTree {
public:
    BinTree(){ 
        root = std::nullptr;
    }
    ~BinTree() {}
    BinTreeNode<T>* root;
};

int main(){
    BinTree<int> tree;
}
Run Code Online (Sandbox Code Playgroud)

我试过在谷歌搜索但没有任何有意义的相关内容 std::nullptr

c++ nullptr

6
推荐指数
1
解决办法
1937
查看次数

nullptr rvalue怎么样?

虽然看着实行nullptr 这里,有什么了我的注意的是,nullptr就是rvalue这意味着我们可以做这样的事情

std::nullptr_t&& nullref = nullptr;
Run Code Online (Sandbox Code Playgroud)

但怎么可能nullptrrvalue因为实现是这样的

const class {...} nullptr = {};
Run Code Online (Sandbox Code Playgroud)

这是核心功能吗?我错过了什么?

c++ rvalue nullptr language-lawyer

6
推荐指数
1
解决办法
505
查看次数

是否需要nullptr_t类型的对象?

引用C ++ 11:(18.2 / 9)

nullptr_t 定义如下:

namespace std { typedef decltype(nullptr) nullptr_t; }
Run Code Online (Sandbox Code Playgroud)

nullptr_t作为同义词的类型具有在3.9.1和4.10中描述的特征。[注意:虽然nullptr不能使用的地址,但是nullptr_t可以使用作为左值的另一个对象的地址。—尾注]

我们是否需要类型的对象nullptr_t(除外nullptr)?

c++ nullptr c++11

6
推荐指数
2
解决办法
135
查看次数

在 C++11 上下文中使用 std::Optional

我正在编写一个小型 C++11 库,我相信std::optional它会是一些可以返回nullptr. 然而,std::optional这是 C++17 的功能。由于 C++11 是一项要求,因此我正在寻找在std::optional保持兼容性的同时使用的方法。

我发现功能宏可以测试。我想我可以用它来检测是否std::optional可用......但是当它不可用时最好的方法是什么?

我应该提供自己的std::optional实现吗?

不可用nullptr时返回?std::optional(可能会弄乱我的代码。)

还是放弃这个想法,nullptr只继续返回?

c++ nullptr c++11 c++17 stdoptional

6
推荐指数
1
解决办法
8563
查看次数

为什么nullptr是核心语言的一部分,而nullptr_t是STL的一部分?

据我所知,它nullptr是核心语言的一部分。

引用C ++ 11:(18.2 / 9)

nullptr_t 定义如下:

namespace std { typedef decltype(nullptr) nullptr_t; }

并且在标头中定义<cstddef>

c++ nullptr c++11

6
推荐指数
2
解决办法
207
查看次数

VS2019 C6011 取消引用空指针“NewNode”时出错

你好?我正在研究双向链表

无论如何,当我过去将数据放在节点上时,出现了 C6011 错误 [:Dereferencing Null Pointer 'NewNode']

所以我参考了https://learn.microsoft.com/ko-kr/visualstudio/code-quality/c6011?view=vs-2019

尝试检查空值。但它没有用..

我检查了错误窗口的一些详细信息,例如我添加的图片

他们说“NewNode 将为 NULL”,“即使取消引用,NewNode 仍将为 NULL”

在此输入图像描述

代码如下

1.头文件(DoubleLinkedList.h)

#pragma once
#ifndef DOUBLY_LINKEDLIST_H
#define DOUBLY_LINKEDLIST_H

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElementType;

typedef struct MyNode
{
    ElementType Data;
    struct MyNode* Prev;
    struct MyNode* Next;
}Node;

Node* DLL_CreateNode(ElementType NewData); //1.create node
void DLL_DestroyNode(Node* Node);//2.destroy node
void DLL_AppendNode(Node** Head, Node* NewNode);//3.append node
/*
4.insert node
connect
prev-insert-next  
*/
void DLL_InsertAfter(Node* Current, Node* NewNode);


void DLL_RemoveNode(Node** Head, Node* Remove);//5.remove node …
Run Code Online (Sandbox Code Playgroud)

c nullptr

6
推荐指数
1
解决办法
5886
查看次数

更新到 macOS 13.3 无法编译 cpp

更新到 Ventura 13.3,安装最新的 Xcode 和命令行工具后,我在编译任何 cpp 文件时收到此错误

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cstddef:50:9: error: no member named 'nullptr_t' in the global namespace
Run Code Online (Sandbox Code Playgroud)

它发生在#include <iostream>

例如:

#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

In file included from Untitled.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:41:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:221:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:18:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/memory:841:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/copy.h:12:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__algorithm/unwrap_iter.h:13:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__iterator/iterator_traits.h:14:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__iterator/incrementable_traits.h:15:
In …
Run Code Online (Sandbox Code Playgroud)

c++ macos nullptr

6
推荐指数
1
解决办法
2342
查看次数