考虑到功能:
template <class T> void f(const T* const ptr);
Run Code Online (Sandbox Code Playgroud)
什么是T对f(nullptr)?
阅读一些代码我发现一个类只接受新的C++ 11 nullptr_t作为参数.该课程如下所示.
我是否纠正了我唯一可以通过专门使用构造对象的东西nullptr?
class CA {
public:
CA(nullptr_t) {}
};
Run Code Online (Sandbox Code Playgroud) 假设以下代码段:
template <class T>
void fct(T* a, T* b){
// do something
}
A a;
fct(&a, nullptr); // Problem here!
Run Code Online (Sandbox Code Playgroud)
这使得麻烦,因为调用参数的数据类型的A*,并nullptr_t因此编译器不能推断出模板参数T.
一般来说,我可以想象几个想法如何解决这个问题:
A* b = nullptr和使用fct(&a, b)fct为一个nullptr案例定义一个带有一个参数的重载fct(&a, static_cast<A*>(nullptr))或者是否有更清晰的解决方案,比如创建类似"typed nullptr"的东西?
我正在学习 C++,我知道“new”关键字用于将内存中的地址分配给指针。我认为当使用“nullptr”时会初始化一个指向任何内容的指针。那是对的吗?参考示例:
//using nullptr
int *x = nullptr; //this is just a pointer that points to nothing and
//will need to be initialized with an address before it
//it can be used. Correct?
//using new
int *x = new int; //this is basically giving x an address in memory. Will the
//address have some residual value stored in it or will
//it contain zero?
Run Code Online (Sandbox Code Playgroud)
你什么时候会使用其中一种而不是另一种?new 仅用于动态内存分配还是还有其他应用?如果你可以先声明一个指向 nullptr 的指针,然后再初始化它,为什么还要初始化它呢?
谢谢您的帮助!
cout << std::is_assignable<int*, std::nullptr_t>::value << endl;
cout << std::is_assignable<int*&, std::nullptr_t>::value << endl;
Run Code Online (Sandbox Code Playgroud)
输出为:0 1
我不明白为什么第一次检查返回false
我可以将nullptr分配给对指针的引用,但是我不能将它分配给原始指针?
这是相反的!
int* p = nullptr;
int*& pref = nullptr;
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,第二个赋值标记了一个错误:
错误:不能非const结合左值参考类型的INT*到 右值类型的INT*
有人可以解释一下发生了什么吗?
据我所知,引用不能为空,但是当我运行这样的代码时:
#include <iostream>
#include <string>
void test(int i, const std::string& s = nullptr) {
std::cout << i << " " << s << std::endl;
}
int main() {
test(1, "test");
test(2);
}
Run Code Online (Sandbox Code Playgroud)
可选参数s可以为null,并生成代码。此外,test(2)运行时,程序将引发异常,而不是打印一些随机字符串。
当我更改s为int之类的基本类型时,它无法编译,因此我认为魔术仍然保留在字符串类中,但是如何?
而且,如何检查是否s为null?如果我使用if(s==nullptr)或if(s.empty()),它将无法编译。
make_shared的CppReference页面说(与make_unique相同)
可能引发std :: bad_alloc或T的构造函数引发的任何异常。如果引发异常,则这些函数无效。
这意味着在失败的情况下可以引发std :: bad_alloc接收。“函数无效”暗含意味着它无法返回nullptr。如果是这种情况,为什么不总是将make_shared / make_unique始终写入try catch块中是一种惯例?
使用make_shared的正确方法是什么?在尝试捕获块内?或检查nullptr?
鉴于cppreference on<=>的示例,我们可以将示例代码简化为:
struct person {
std::string name;
std::string surname;
auto operator <=> (const person& p) const {
if (const auto result = name <=> p.name; result != 0) {
return result;
} else {
return surname <=> p.surname;
}
}
};
Run Code Online (Sandbox Code Playgroud)
但是我的 IDE(CLion 2020.2)通过 clang-tidy 警告result != 0实际上应该是result != nullptr. 我不知道我们可以std::###_ordering与nullptr. Cpprefence 还声称我们应该将它与literal 进行比较0。那里什么都nullptr没有。
这段代码:
int main() {
person p1{"ccc", "vvv"};
person p2{"aaa", "zzz"}; …Run Code Online (Sandbox Code Playgroud) 我有一个const z* zs = nullptr;
我想转换zs 为std::span
当我尝试这样做时,std::span<const z>(zs) 我收到一条错误消息
\n\n错误:没有匹配的函数可调用 \xe2\x80\x98std::span::span(const z* const&)\xe2\x80\x99
\n
如何转换为 zsstd::span
我试过std::span<const z>(zs[0])了,好像可以编译。这种方式正确吗?
nullptr ×10
c++ ×9
c++11 ×4
templates ×3
auto ×1
c++20 ×1
exception ×1
initializer ×1
new-operator ×1
pointers ×1
reference ×1
std-span ×1
stdstring ×1
type-traits ×1