我用C++编写了一个Python模块,并将其构建为共享对象库,并且工作正常.但是在计算出所有这些的同时,我注意到(通过strace)Python会查找一些不同的变体import.特别是,当我说import foo,Python按顺序搜索:
除了foomodule.so之外,这一切都是可以理解的.为什么Python会以name.so和namemodule.so的形式查找所有内容?这是一些历史文物吗?我搜索了很多,根本没有解释,我想知道我是否应该命名我的模块foomodule.so而不是foo.so. 我的系统似乎在每个约定之后都有一些现有的Python模块,所以我不禁想知道不同的名字是否意味着什么.
这是我的代码:
int f(double x, double y)
{
return std::isnan(x) || std::isnan(y);
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是C而不是C++,只需替换std::为__builtin_(不要简单地删除std::,原因如下所示:为什么GCC对C++ <cmath>比C <math.h>更有效地实现isnan()?).
这是集会:
ucomisd %xmm0, %xmm0 ; set parity flag if x is NAN
setp %dl ; copy parity flag to %edx
ucomisd %xmm1, %xmm1 ; set parity flag if y is NAN
setp %al ; copy parity flag to %eax
orl %edx, %eax ; OR one byte of each result into a full-width register
Run Code Online (Sandbox Code Playgroud)
现在让我们尝试另一种做同样事情的配方:
int f(double x, …Run Code Online (Sandbox Code Playgroud) 我在尝试创建一个从一个继承自定义纯虚函数的类的类继承的对象时遇到错误.我不确定是什么问题.我知道我需要覆盖派生类中的纯虚函数,但它不起作用.我只想覆盖我的ProduceItem类中的函数而不是我的Celery类,因为我希望Celery类继承ProduceItem中的重写方法.
在主要:
GroceryItem *cel = new Celery(1.5); //Cannot cast 'Celery' to its private base class GroceryItem
class GroceryItem
{
public:
virtual double GetPrice() = 0;
virtual double GetWeight() = 0;
virtual std::string GetDescription() = 0;
protected:
double price;
double weight;
std::string description;
};
Run Code Online (Sandbox Code Playgroud)
ProduceItem头文件:
#include "GroceryItem.h"
class ProduceItem : public GroceryItem
{
public:
ProduceItem(double costPerPound);
double GetCost();
double GetWeight();
double GetPrice();
std::string GetDescription();
protected:
double costPerPound;
};
Run Code Online (Sandbox Code Playgroud)
ProduceItem.cpp文件:
#include <stdio.h>
#include "ProduceItem.h"
ProduceItem::ProduceItem(double costPerPound)
{
price = costPerPound * weight; …Run Code Online (Sandbox Code Playgroud) 在C++ 11中,我们可以使用"brace-or-equal-initializer"(标准中的单词)进行类内初始化,如下所示:
struct Foo
{
/*explicit*/ Foo(int) {}
};
struct Bar
{
Foo foo = { 42 };
};
Run Code Online (Sandbox Code Playgroud)
但如果我们不发表评论explicit,它就不再编译了.GCC 4.7和4.9说:
error: converting to ‘Foo’ from initializer list would use explicit constructor ‘Foo::Foo(int)’
Run Code Online (Sandbox Code Playgroud)
我发现这令人惊讶.C++ 11标准真的是这个代码无法编译的意图吗?
删除它=修复它:Foo foo { 42 };但我个人觉得更难以向那些已经习惯了这种形式的人解释=几十年,而且由于标准指的是"支撑或平等初始化器",因此很好的旧方式在这种情况下不起作用.
c++ explicit-constructor initializer-list in-class-initialization c++11
我有一个Python生成器,可以调用自己来获得更多元素.它看起来像这样:
def gen(list):
# ...
if list:
for x in gen(list[1:]):
yield x
Run Code Online (Sandbox Code Playgroud)
我的问题是关于最后两行:是否有更简洁的方式来表达这一点?我希望有这样的东西(理解这不是有效的Python原样):
def gen(list):
# ...
if list:
yield each in gen(list[1:])
Run Code Online (Sandbox Code Playgroud) 受到另一个问题的启发,我想以便携方式检索Python解释器的完整命令行.也就是说,我想原来argv解释的,而不是sys.argv不包括选项来解释本身(如-m,-O等).
sys.flags告诉我们设置了哪些布尔选项,但它没有告诉我们有关-m参数的信息,并且标志集必然会随着时间的推移而发生变化,从而产生维护负担.
在Linux上,您可以使用procfs来检索原始命令行,但这不是可移植的(并且它有点粗略):
open('/proc/{}/cmdline'.format(os.getpid())).read().split('\0')
Run Code Online (Sandbox Code Playgroud) 即使我从msdn看到sendinput函数,我也不知道输入了什么参数.
UINT WINAPI SendInput(
_In_ UINT nInputs,
_In_ LPINPUT pInputs,
_In_ int cbSize
);
Run Code Online (Sandbox Code Playgroud)
上面的参数意味着什么,我需要为它们创建什么?另外,键入,ki.wScan,ki.time,ki.dwExtraInfo,ki.wVk,ki.dwFlags上面的对象是什么意思,还有其他可能经常使用的对象吗?
我有一些进程显示<defunct>在top(和ps)中.我从真实的脚本和程序中榨取了一些东西.
在我的crontab:
* * * * * /tmp/launcher.sh /tmp/tester.sh
Run Code Online (Sandbox Code Playgroud)
的内容launcher.sh(这是当然的标记为可执行):
#!/bin/bash
# the real script does a little argument processing here
"$@"
Run Code Online (Sandbox Code Playgroud)
的内容tester.sh(这是当然的标记为可执行):
#!/bin/bash
sleep 27 & # the real script launches a compiled C program in the background
Run Code Online (Sandbox Code Playgroud)
ps 显示以下内容:
user 24257 24256 0 18:32 ? 00:00:00 [launcher.sh] <defunct>
user 24259 1 0 18:32 ? 00:00:00 sleep 27
Run Code Online (Sandbox Code Playgroud)
请注意,tester.sh它不会出现 - 它在启动后台作业后退出.
为什么要launcher.sh坚持,标记<defunct> …
我正在做一个小项目,总共约3-4人.我希望有一个可靠的方法来调试应用程序,例如日志.是否有任何关于如何构建它的好资源?我从项目经理那里听到很多很好的日志记录功能对每个项目都很重要,但我不知道该怎么做.
考虑以下类:
template <class Derived>
class BaseCRTP {
private:
friend class LinkedList<Derived>;
Derived *next = nullptr;
public:
static LinkedList<Derived> instances;
BaseCRTP() {
instances.insert(static_cast<Derived *>(this));
}
virtual ~BaseCRTP() {
instances.remove(static_cast<Derived *>(this));
}
};
Run Code Online (Sandbox Code Playgroud)
struct Derived : BaseCRTP<Derived> {
int i;
Derived(int i) : i(i) {}
};
Run Code Online (Sandbox Code Playgroud)
int main() {
Derived d[] = {1, 2, 3, 4};
for (const Derived &el : Derived::instances)
std::cout << el.i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我知道,这是不确定的行为来访问的成员Derived在BaseCRTP<Derived>构造函数(或析构函数),因为Derived构造函数执行后的BaseCRTP<Derived>构造(和析构函数的其他方式)。
我的问题是:在不访问任何成员的 …