小编Wae*_*elJ的帖子

GNU并行完全没有工作

我一直试图使用GNU并行一段时间,但我从来没有能够让它完全运行!

例如,运行(在非空目录中!):

ls | parallel echo            # Outputs single new line
ls | parallel echo echo echo  # Outputs three new lines.
ls | parallel echo {}         # /bin/bash: {}: command not found
ls | parallel echo '{}'       # /bin/bash: {}: command not found
ls | parallel 'echo {}'       # Outputs: {}
ls | parallel -IMM 'echo MM'  # Outputs: MM
Run Code Online (Sandbox Code Playgroud)

它似乎只是将每个参数作为命令执行,这没有任何意义.

我试过bash,zsh,tcsh,csh和sh,但无济于事.

parallel-processing gnu xargs gnu-parallel

52
推荐指数
2
解决办法
2万
查看次数

Makefile中的别名目标名称

问题:

是否可以为目标指定不同的名称或别名,以便可以使用原始目标名称或别名来调用它.

比如像

/very/long/path/my_binary: dep_a dep_b dep_c
    # Compile

# Desired command
ALIAS my_binary = /very/long/path/my_binary

# NOTE: Notice the use of 'my_binary' in the dependencies
data1: my_binary datafile
    # Build data file using compiled my_binary
Run Code Online (Sandbox Code Playgroud)

尝试1:.PHONY

我尝试过使用.PHONY目标:

.PHONY: my_binary
my_binary: /very/long/path/my_binary
Run Code Online (Sandbox Code Playgroud)

从命令行调用时,这很有用:

# Runs rule 'my_binary' and then *only* runs rule '/very/long/path/my_binary'
# if the rule '/very/long/path/my_binary' needs updating.
make my_binary
Run Code Online (Sandbox Code Playgroud)

但是,当别名my_binary列为依赖项时,这不起作用:

# *Always* thinks that rule 'data1' needs updating, because it always thinks that …
Run Code Online (Sandbox Code Playgroud)

alias makefile synonym target

23
推荐指数
3
解决办法
1万
查看次数

在Python中对切片进行高效迭代

Python中切片操作的迭代效率如何?如果切片不可避免地复制,是否有替代方案?

我知道列表上的切片操作是O(k),其中k是切片的大小.

x[5 : 5+k]  # O(k) copy operation
Run Code Online (Sandbox Code Playgroud)

然而,当迭代列表的一部分时,我发现最干净(和大多数Pythonic?)的方式(不必求助于索引)是这样做的:

for elem in x[5 : 5+k]:
  print elem
Run Code Online (Sandbox Code Playgroud)

然而,我的直觉是,这仍然会导致子列表的昂贵副本,而不是简单地迭代现有列表.

python iteration performance slice

10
推荐指数
2
解决办法
1万
查看次数

Clang:模板演绎失败'双'与'<双>'

请考虑以下代码,该代码使用带有可变参数的函数:

#include <iostream>

// Typedef function type
template<typename... Output>
using Func = void(Output*...);

// Function runner
template<typename... Output>
void run_func(Func<Output...>& func, Output*... output) {
  for (int i=0 ; i < 10 ; ++i) {
    func(output...);
  }
}

void f(double* d) {
  *d *= 2;
};

int main() {
  double value = 1.0;
  run_func(f, &value);
  printf("%f\n", value);
}
Run Code Online (Sandbox Code Playgroud)

用g ++ 4.7.3编译它可以正常工作,并且运行1024.0按预期生成.

使用icpc 14.0.2进行编译会使其崩溃...

templ.cc(21): internal error: assertion failed: lower_expr: bad kind (shared/cfe/edgcpfe/lower_il.c, line 18582)

    run_func(f, &value);
    ^
Run Code Online (Sandbox Code Playgroud)

使用clang 3.5.0-1进行编译会出现以下错误消息: …

c++ templates clang function-templates c++11

10
推荐指数
1
解决办法
387
查看次数

在'this'指针上使用placement new是否安全

目前的实施

我有一个包含unique_ptr相互依赖的字段的类:

class ResourceManager {
  ResourceManager() {}

  ResourceManager(A* a_ptr) :
    b_ptr(new B(a)),
    c_ptr(new C(b_ptr.get())) {}

  ResourceManager& operator=(ResourceManager&& that) {
    // Call destructor, then construct a new instance on top
    ~ResourceManager();
    ResourceManager* new_this = new(this) ResourceManager();

    // Surely this must be the case, right?
    // Is there any reason to prefer using either?
    assert(new_this == this);

    new_this->b_ptr = that.b_ptr;
    new_this->c_ptr = that.c_ptr;

    return *new_this;
  }

  unique_ptr<B> b;
  unique_ptr<C> c;
};
Run Code Online (Sandbox Code Playgroud)

用例

这里的用例是我想将新值重新分配给指针,同时保持ResourceManager作为堆栈分配的变量,或者作为非指针类成员.

使用我当前的设置,我想像使用这样的东西:

A a, another_a;
ResourceManager …
Run Code Online (Sandbox Code Playgroud)

c++ raii placement-new explicit-destructor-call

6
推荐指数
1
解决办法
549
查看次数

Java:GregorianCalendar的最大值和最小值是什么/在哪里?

GregorianCalendar的最大值和最小值是多少?

它们是一个常数,如Integer.MAX_VALUE,还是GregorianCalendar.get(BLAH)?

简而言之,如何创建具有最小/最大值的GregorianCalendar实例?

java calendar gregorian-calendar

3
推荐指数
2
解决办法
2万
查看次数

手动调用析构函数并重用内存

虽然我知道这可能不是最好的想法,但我假设:

(即定义行为)手动调用对象的析构函数,然后将内存重用于另一个对象是否合法?

定义:

class A {
  int a, b, c;
  A() {}
  ~A() {}
}

A createObject() {
  A object;
  return object;
}
Run Code Online (Sandbox Code Playgroud)

码:

A* object = new A();
// Use object...
object->~A();
*object = createObject();
Run Code Online (Sandbox Code Playgroud)

c++ destructor explicit-destructor-call

2
推荐指数
1
解决办法
346
查看次数

C++有一个像unique_ptr这样的智能指针,带有"构造前的破坏"语义吗?

问题

考虑这个简单的类:

struct C {
   C(const char* name) : name(name) {
         cout << "constructing " << name  << endl;
   }

   ~C() {
       cout << "destructing " << name << endl;
   }

   string name;
};
Run Code Online (Sandbox Code Playgroud)

我想有一个指向这个类的实例的指针,它通常被另一个实例替换.但是,我希望创建新实例之前销毁当前实例.

错误的例子

如果我unique_ptr以正常方式使用a ,这不起作用:

unique_ptr<C> c( new C("the first one"));
c.reset(new C("the second one"));
Run Code Online (Sandbox Code Playgroud)

(不受欢迎的)输出:

构建第一个

构建第二个

破坏第一个

破坏第二个

丑陋的例子

可以如下实现期望的效果:

unique_ptr<C> c( new C("the first one"));
c.reset();  // explicitly destruct the first one first
c.reset(new C("the second one"));
Run Code Online (Sandbox Code Playgroud)

输出: …

c++ smart-pointers resource-management resourcemanager unique-ptr

1
推荐指数
1
解决办法
116
查看次数