是否可以将lambda函数作为函数指针传递?如果是这样,我必须做错了,因为我收到编译错误.
请考虑以下示例
using DecisionFn = bool(*)();
class Decide
{
public:
Decide(DecisionFn dec) : _dec{dec} {}
private:
DecisionFn _dec;
};
int main()
{
int x = 5;
Decide greaterThanThree{ [x](){ return x > 3; } };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我得到以下编译错误:
In function 'int main()':
17:31: error: the value of 'x' is not usable in a constant expression
16:9: note: 'int x' is not const
17:53: error: no matching function for call to 'Decide::Decide(<brace-enclosed initializer list>)'
17:53: note: candidates are: …Run Code Online (Sandbox Code Playgroud) 就编译器优化而言,将堆分配更改为堆栈分配是否合法和/或可能?或者会破坏as-if规则?
例如,假设这是代码的原始版本
{
Foo* f = new Foo();
f->do_something();
delete f;
}
Run Code Online (Sandbox Code Playgroud)
编译器是否能够将此更改为以下内容
{
Foo f{};
f.do_something();
}
Run Code Online (Sandbox Code Playgroud)
我不这么认为,因为如果原始版本依赖于自定义分配器之类的东西,那将会产生影响.标准是否对此有具体说明?
编译以下代码片段时(clang x86-64 -O3)
std::array<int, 5> test()
{
std::array<int, 5> values {{0, 1, 2, 3, 4}};
return values;
}
Run Code Online (Sandbox Code Playgroud)
它产生了我期望的典型装配
test(): # @test()
mov rax, rdi
mov ecx, dword ptr [rip + .L__const.test().values+16]
mov dword ptr [rdi + 16], ecx
movups xmm0, xmmword ptr [rip + .L__const.test().values]
movups xmmword ptr [rdi], xmm0
ret
.L__const.test().values:
.long 0 # 0x0
.long 1 # 0x1
.long 2 # 0x2
.long 3 # 0x3
.long 4 # 0x4
Run Code Online (Sandbox Code Playgroud)
但是对于小型阵列,似乎已经找到了窍门?
std::array<int, 3> test()
{ …Run Code Online (Sandbox Code Playgroud) 作为这个问题的后续,我有一个函数将返回numpy.array固定列但可变行的二维
import numpy.typing as npt
def example() -> npt.ArrayLike:
data = np.array([[1,2,3],
[4,5,6],
...,
[x,y,z]])
Run Code Online (Sandbox Code Playgroud)
我如何具体提示返回的数组将是3由N(变量)行组成的列?
我有一个dataframe来自外部源(csv文件)的众多列(≈30),但其中有几列没有值或总是相同.因此,我会很快看到value_counts每一列,我该怎么做?
例如
Id, temp, name
1 34, null, mark
2 22, null, mark
3 34, null, mark
Run Code Online (Sandbox Code Playgroud)
会给我一个说明这个的对象
所以我会知道temp是无关紧要的,名字不是很有趣(总是一样的)
当作为可变参数传递时,是否有一种标准的方法来获得变量的类型大小?
auto x = ...;
auto y = sizeof(promoted(x));
Run Code Online (Sandbox Code Playgroud)
结果应该是:
char -> sizeof(int)
int -> sizeof(int)
float -> sizeof(double)
...
Run Code Online (Sandbox Code Playgroud) 我正在使用scrapy来抓取一个网站,该网站似乎将随机值附加到每个网址末尾的查询字符串中.这使得爬行成为一种无限循环.
如何让scrapy忽略URL的查询字符串部分?
def choose_option(self):
if self.option_picker.currentRow() == 0:
description = open(":/description_files/program_description.txt","r")
self.information_shower.setText(description.read())
elif self.option_picker.currentRow() == 1:
requirements = open(":/description_files/requirements_for_client_data.txt", "r")
self.information_shower.setText(requirements.read())
elif self.option_picker.currentRow() == 2:
menus = open(":/description_files/menus.txt", "r")
self.information_shower.setText(menus.read())
Run Code Online (Sandbox Code Playgroud)
我正在使用资源文件,当我在打开函数中使用它作为参数时出现问题,但是当我使用它来加载图片和图标时,一切都很好.
如果我有std::ofstream可能打开或未打开的,是否可以安全地尝试close?在其他方面确实close()做任何讨厌(抛出异常等),如果!is_open().例如
std::ofstream out;
if (some_condition)
{
out.open(path, std::ios::out);
}
Run Code Online (Sandbox Code Playgroud)
完成文件后,我可以说
out.close();
Run Code Online (Sandbox Code Playgroud)
或者我应该先检查一下
if (out.is_open())
out.close();
Run Code Online (Sandbox Code Playgroud)
std::basic_fstream::close关于cppreference 的唯一描述是
关闭相关文件.
有效地打电话rdbuf()->close().如果在操作期间发生错误,setstate(failbit)则调用.
我有一个清单
users = ['tom', 'dick', 'harry']
Run Code Online (Sandbox Code Playgroud)
在Jinja模板中,我想打印除tom加入之外的所有用户的列表.我无法在将变量传递给模板之前对其进行修改.
我尝试了列表理解,并使用Jinja的reject过滤器,但我无法让这些工作,例如
{{ [name for name in users if name != 'tom'] | join(', ') }}
Run Code Online (Sandbox Code Playgroud)
给出语法错误.
如何有条件地加入列表项?