有人告诉我,以下代码在 C++20 之前具有未定义的行为:
int *p = (int*)malloc(sizeof(int));
*p = 10;
Run Code Online (Sandbox Code Playgroud)
真的吗?
论点是int
对象的生命周期在为其分配值之前没有开始(P0593R6)。要解决此问题,new
应使用放置:
int *p = (int*)malloc(sizeof(int));
new (p) int;
*p = 10;
Run Code Online (Sandbox Code Playgroud)
我们真的必须调用一个微不足道的默认构造函数来启动对象的生命周期吗?
同时,代码在纯 C 中没有未定义的行为。但是,如果我int
在 C 代码中分配 an并在 C++ 代码中使用它呢?
// C source code:
int *alloc_int(void)
{
int *p = (int*)malloc(sizeof(int));
*p = 10;
return p;
}
// C++ source code:
extern "C" int *alloc_int(void);
auto p = alloc_int();
*p = 20;
Run Code Online (Sandbox Code Playgroud)
它仍然是未定义的行为吗?
草案-ietf-hybi-thewebsocketprotocol-17的 1.3节"开放式握手"中描述Sec-WebSocket-Key
如下:
为了证明接收到握手,服务器必须获取两条信息并将它们组合以形成响应.第一条信息来自| Sec-WebSocket-Key | 客户端握手中的头字段:
Run Code Online (Sandbox Code Playgroud)Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
对于此头字段,服务器必须获取值(如头字段中所示,例如base64编码的[RFC4648]版本减去任何前导和尾随空格),并将其与全局唯一标识符(GUID,[RFC4122]连接起来. ])字符串形式的"258EAFA5-E914-47DA-95CA-C5AB0DC85B11",不太可能被不了解WebSocket协议的网络端点使用.然后在服务器的握手[FIPS.180-2.2002]中返回此串联的SHA-1散列(160位),base64编码(参见[RFC4648]的第4节).
这是我无法理解的事情:为什么不简单地返回代码101?如果正确使用Sec-WebSocket-Key
是为了安全性,或者为了证明它们可以处理websocket请求,那么任何服务器都可以返回预期的密钥,并假装它们是WebSocket服务器.
我始终认为私有继承仅仅意味着类型不会告诉外部它是从某个基类继承的。不过,似乎还有更多的限制。
考虑以下最小示例:
struct MyInterface {};
struct MyImpl : private MyInterface {};
struct Inherited : public MyImpl {
// Error: 'MyInterface' not accessible because 'MyImpl' uses 'private' to inherit from 'MyInterface'
void doSomething(MyInterface* mi) {}
};
struct Noninherited {
// All fine!
void doSomething(MyInterface* mi) {}
};
Run Code Online (Sandbox Code Playgroud)
Clang、GCC 和 MSVC 都拒绝此代码。鉴于我之前的假设,我本以为一切都会好起来的。
doSomething
只是需要一个指向 的指针MyInterface
,但它不会告诉外界其继承层次结构中Inherited
有哪些MyInterface
内容。在我看来,私有继承不仅不告诉外界继承结构,反而使整个继承结构完全“忘记”继承类型的存在。
这是理解私有继承的正确“思维模型”吗?还有其他意想不到的限制吗?
今天我学习了一些C++基础知识并开始了解wchar_t
.我无法弄清楚,为什么我们实际上需要这种数据类型,我该如何使用它?
根据这个答案,iterator
必须可以隐含地转换为const_iterator
.insert_or_assign()
既然如此,正如我们可以看到的那样,为什么在C++ 17中添加了新的签名std::map::erase()
?
在C++ 11中,我们有 iterator erase( const_iterator pos );
在C++ 17中,我们现在有了 iterator erase( iterator pos );
是不是C++ 11签名足以接收iterator
和const_iterator
?
我已经在各种链接上搜索过这个,但仍然存在疑问.
我不明白LocalAlloc
vs GlobalAlloc
vs malloc
vs new
内存分配的区别.
我已经浏览了MSDN的这个链接:
请解释以下声明:
该的malloc函数有被运行时依赖的缺点.在新的运营商有被依赖编译器和语言相关的缺点
GCC和Clang都拒绝接受以下代码中的C风格演员.
http://coliru.stacked-crooked.com/a/c6fb8797d9d96a27
struct S {
typedef const int* P;
operator P() { return nullptr; }
};
int main() {
int* p1 = const_cast<int*>(static_cast<const int*>(S{}));
int* p2 = (int*)(S{});
}
Run Code Online (Sandbox Code Playgroud)
main.cpp: In function 'int main()': main.cpp:7:25: error: invalid cast from type 'S' to type 'int*' int* p2 = (int*)(S{}); main.cpp:7:15: error: cannot cast from type 'S' to pointer type 'int *' int* p2 = (int*)(S{}); ^~~~~~~~~~~
但是,根据标准,C风格的演员表可以执行a static_cast
后跟a 执行的转换const_cast
.这段代码是否格式良好?如果没有,为什么不呢?
void foo() try {} catch (...) {}
// OK, function-try-block
Run Code Online (Sandbox Code Playgroud)
[]() try {} catch (...) {} ();
// error: expected ‘{’ before ‘try’
Run Code Online (Sandbox Code Playgroud)
[]() { try {} catch (...) {} } ();
// OK, extra curly braces`
Run Code Online (Sandbox Code Playgroud)
为什么不允许第二种变体?
在cppreference上,我们可以看到std::optional
采用默认值U&&
而不是T&&
。
它使我无法编写以下代码:
std::optional<std::pair<int, int>> opt;
opt.value_or({1, 2}); // does not compile
opt.value_or(std::make_pair(1, 2)); // compiles
Run Code Online (Sandbox Code Playgroud)
但是,我发现使用 没有任何好处U&&
,因为U
必须可以转换为T
此处。
所以,考虑下面的代码,如果我们有一些U
不同于 的类型T
,那么就不会有完美的匹配。然而,通过执行隐式转换,我们仍然可以解析我们的调用:
template< class U >
constexpr T value_or( T&& default_value ) const&;
Run Code Online (Sandbox Code Playgroud)
我有以下代码来测试模板函数是否可以接受需要额外隐式转换才能完美匹配的参数,并且它可以编译:
#include <cstdio>
#include <optional>
#include <map>
struct A {
int i = 1;
};
struct B {
operator A() const {
return A{2};
}
};
template <typename T>
struct C { …
Run Code Online (Sandbox Code Playgroud) 我试图理解开放寻址方法.我参考TH Cormen关于这个主题的书,其中指出在开放式寻址中删除是很困难的.我完全陷入这一段:
从开放地址哈希表中删除很困难.当我们从插槽中删除密钥时
i
,我们不能简单地通过存储将该插槽标记为空NIL
.这样做可能会导致无法检索任何键,k
在插入过程中我们已探测到插槽i
并发现它已被占用.
我不明白这一点.请用一些例子解释一下.
c++ ×8
c++11 ×2
c++17 ×2
algorithm ×1
c++14 ×1
c++20 ×1
hash ×1
hashtable ×1
heap-memory ×1
inheritance ×1
iterator ×1
malloc ×1
stdoptional ×1
websocket ×1
winapi ×1