我在使用 g++-12 时遇到错误,但在 clang++-13 中不会发生该错误。特别是,这段代码:
\nstruct A {\n constexpr virtual ~A() = default;\n constexpr A() = default;\n};\n\nstruct B : public A {\n constexpr ~B() = default;\n constexpr B() = default;\n};\n\nconstexpr int demo(){\n B *b = new B();\n delete b;\n return 2;\n}\n\nint main(){\n constexpr int demod = demo();\n return demod;\n}\n
Run Code Online (Sandbox Code Playgroud)\n使用 clang++ 编译,但使用 g++ 编译会出现错误:
\nstruct A {\n constexpr virtual ~A() = default;\n constexpr A() = default;\n};\n\nstruct B : public A {\n constexpr ~B() = default;\n constexpr B() …
Run Code Online (Sandbox Code Playgroud) 我发现在 C++ 中我们可以+
在 lambda 函数中使用+[]{}
文章中的示例:
#include <iostream>
#include <type_traits>
int main()
{
auto funcPtr = +[] {};
static_assert(std::is_same<decltype(funcPtr), void (*)()>::value);
}
Run Code Online (Sandbox Code Playgroud)
lambda中+号的主要思想
您可以通过在前面添加 + 来强制编译器将 lambda 生成为函数指针而不是闭包,如上所示。
但是在 lambda 中使用“+”有什么好处呢?您能否提供示例或将我链接到解释?
给定一个 C++11 编译器,#error
它最终应该使用哪个正确的编译器?
// no #includes!
#define SOMEMACRO true
#if SOMEMACRO
#error "it was true"
#else
#error "it was false"
#endif
Run Code Online (Sandbox Code Playgroud)
显然我#error
只是作为测试使用。我知道true
并false
以适当的语言定义,但这是预处理器上下文。在 C99 中,它似乎不被预处理器识别。
我问是因为似乎我尝试过的所有编译器都将其视为“真”,而静态代码分析工具坚持认为true
未定义,隐含错误并以“它是假的”结束。
我决定为 YouTube 实时聊天创建一个用户脚本。这是代码:
const toString = Function.prototype.toString
unsafeWindow.setTimeout = function (fn, t, ...args) {
unsafeWindow.console.log(fn, fn.toString(), toString.call(fn))
unsafeWindow.fns = (unsafeWindow.fns ?? []).concat(fn)
return setTimeout(fn, t, ...args)
}
Run Code Online (Sandbox Code Playgroud)
现在看看输出的样子:
一些函数的输出是可以预测的,但看看其他函数!当你这样做console.log
时,你会看到函数体,但如果你调用fn.toString()
,你会看到function () { [native code] }
。
但为什么?脚本在页面之前加载,因此 YouTube 的脚本无法替换这些方法。
假设我有非常大的字节对象(加载二进制文件后),我想逐部分读取并推进起始位置直到它到达末尾。我使用切片来实现这一点。我担心每次我请求切片时 python 都会创建全新的副本,而不是简单地给我指向我想要的位置的内存地址。
简单的例子:
data = Path("binary-file.dat").read_bytes()
total_length = len(data)
start_pos = 0
while start_pos < total_length:
bytes_processed = decode_bytes(data[start_pos:]) # <---- ***
start_pos += bytes_processed
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,python 是否由于start_pos
切片而从 开始创建全新的字节对象副本。如果是这样,避免数据复制并仅使用指针传递到字节数组的相关位置的最佳方法是什么。
我写这个代码:
#include <iostream>
using namespace std;
class Foo
{
public:
int a = 0;
Foo()
{
cout << "ctor: " << this << endl;
}
~Foo() {
cout << "dtor: " << this << endl;
}
};
Foo f()
{
Foo foo;
cout << "f " << &foo << endl;
return foo;
}
void ff(Foo &&ffoo)
{
cout << "ff " << &ffoo << endl;
}
int main()
{
ff(f());
std::cout << "Hello World!\n";
}
Run Code Online (Sandbox Code Playgroud)
输出看起来不错:
ctor: 0x7ffeda89bd7c
f 0x7ffeda89bd7c …
Run Code Online (Sandbox Code Playgroud) 我有一个友元函数模板operator<<
,它可以与 gcc 和 clang 一起使用,但不能与 msvc 一起使用。
#include <iostream>
#include <type_traits>
template< typename T, std::enable_if_t< T{1}, int> =0 >
class Foo
{
template< typename Ar, typename R>
friend Ar& operator<<(Ar& os, const Foo<R>& foo)
{
return os;
}
};
int main()
{
Foo<int> i;
std::cout << i; //works with gcc and clang but does not compile with msvc
}
Run Code Online (Sandbox Code Playgroud)
我想知道哪个编译器根据 C++ 标准具有正确的行为。msvc 错误说:
<source>(4): error C2972: 'Foo': template parameter 'unnamed-parameter': the type of non-type argument is invalid …
Run Code Online (Sandbox Code Playgroud) 我想在 Typescript 中使用 WeakRef。我尝试使用目前可用的最后一个版本(4.1.5)。我有一个编译错误:
const toIdString = (o: Object): string =>
o.constructor.name + JSON.stringify(o);
export class MemoCache {
static handle<T>(o: T): T {
const id = toIdString(o);
const cached = MemoCache.map.get(id);
if (cached) {
return cached.deref() as T;
}
MemoCache.map.set(id, new WeakRef(o));
return o;
}
static map = new Map<string, WeakRef>();
}
Run Code Online (Sandbox Code Playgroud)
我有编译错误。
src/Memoizer.ts:11:31 - error TS2304: Cannot find name 'WeakRef'.
11 MemoCache.map.set(id, new WeakRef(o));
~~~~~~~
src/Memoizer.ts:15:32 - error TS2304: Cannot find name 'WeakRef'.
15 static map = new Map<string, …
Run Code Online (Sandbox Code Playgroud) 在 template 的以下定义中struct B
,lambda 用作非类型模板参数的默认值,并且在 lambda 的主体中A
定义了某种类型:
template <auto = []{ struct A{}; }>
struct B {};
Run Code Online (Sandbox Code Playgroud)
Clang 和 MSVC 都同意这个定义,但 GCC 抱怨道:
错误:
struct<lambda()>::A
模板参数列表中“”的定义
演示: https: //gcc.godbolt.org/z/f1dxGbPvs
这里是哪个编译器?
在我的应用程序中,我将请求从客户端发送到 node.js 服务器。服务器发送包含脚本的 HTML 响应:
\napp.post(\'/verify\', cors(issue2options), async (req, res) => {\n let auth = await mongo({\n input: \'express app\',\n data: req.body[\'auth\'][\'address\'],\n type: \'verify\'\n }, \'get\', \'type\')\n\n if (empty(auth)) {\n res.send(JSON.stringify(`\n <div id="registration" slot="varan-about">\n \xd0\x92\xd0\xbe\xd0\xb9\xd0\xb4\xd0\xb8\xd1\x82\xd0\xb5 \xd0\xbf\xd0\xbe\xd0\xb4 \xd0\xb0\xd0\xba\xd0\xba\xd0\xb0\xd1\x83\xd0\xbd\xd1\x82\xd0\xbe\xd0\xbc \xd0\xb0\xd0\xb4\xd0\xbc\xd0\xb8\xd0\xbd\xd0\xb8\xd1\x81\xd1\x82\xd1\x80\xd0\xb0\xd1\x82\xd0\xbe\xd1\x80\xd0\xb0\n <script type="module">\n console.log(\'sssss\');\n </script>\n </div>\n `));\n } else {\n res.send(true)\n }\n})\n
Run Code Online (Sandbox Code Playgroud)\n在客户端上,我将响应注入document.body
使用insertAdjacentHTML
:
document.body.insertAdjacentHTML(\'afterbegin\', json);\n
Run Code Online (Sandbox Code Playgroud)\n但脚本不执行。
\n是否可以使用此方法注入脚本?
\nc++ ×6
javascript ×2
lambda ×2
templates ×2
c ×1
c++11 ×1
c++20 ×1
console.log ×1
constexpr ×1
function ×1
html ×1
node.js ×1
preprocessor ×1
python ×1
rvalue ×1
tostring ×1
typescript ×1