小编Elm*_*der的帖子

在C++模板实例化中查找依赖名称

当我尝试编译此代码时

// void foobar(int); 

template <class T>
struct Foo {
  void bar(T t) { foobar(t); };
};

void foobar(int);

template class Foo<int>;
Run Code Online (Sandbox Code Playgroud)

使用g ++ 4.8.2我收到以下错误消息

foo.cc: In instantiation of ‘void Foo<T>::bar(T) [with T = int]’:
foo.cc:10:16:   required from here
foo.cc:5:27: error: ‘foobar’ was not declared in this scope, and no 
             declarations were found by argument-dependent lookup at 
               the point of instantiation [-fpermissive]
   void bar(T t) { foobar(t); };
                           ^
foo.cc:8:6: note: ‘void foobar(int)’ declared here, later in the translation unit …
Run Code Online (Sandbox Code Playgroud)

c++ templates language-lawyer argument-dependent-lookup

19
推荐指数
2
解决办法
1192
查看次数

解决Python包中的错误

我最近遇到了以下问题:我正在开发一个Python(称为spuq)数值库scipy,其核心需求.现在scipy中的一个函数,它被调用btdtri,有一个输入参数元组的错误.然而,根据scipy的开发人员,这个bug现在已经在scipy 0.9版中修复了.所以在我的代码中我有这样的东西:

import scipy

def foo(a, b, c):
  if scipy.__version__>=(0, 9):
      return btdtri(a, b, c)
  else:
      return my_fixed_btdtri(a, b, c)
Run Code Online (Sandbox Code Playgroud)

但这样做,我真的不喜欢丢弃我的代码与第三方软件包的错误修复.我希望包含在一个模块中,它实现了变通方法,并让我的所有其他模块自动使用修补模块.

现在我的问题是:一般来说处理这类案件的最佳做法是什么?比如说我自己spuq.contrib.scipy说并在那里说

from scipy import *
if __version__ < (0, 9):
  btdtri = my_fixed_btdtri
Run Code Online (Sandbox Code Playgroud)

而不是到处导入scipy导入spuq.contrib.scipy?我认为这很复杂,容易忘记(可能是单声道和丑陋的).也许有一种方法可以自动"挂钩"加载包并scipy直接修改模块,这样每个其他包只能看到修补后的包?我认为这个问题很常见,所以可能应该有一些"最佳实践".

python package

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

使用WeakValueDictionary进行缓存时GC的问题

根据weakref模块的官方Python文档,"弱引用的主要用途是实现保存大对象的缓存或映射,......".因此,我使用WeakValueDictionary为长时间运行的函数实现缓存机制.然而,事实证明,缓存中的值从未停留在那里,直到实际再次使用它们,但几乎每次都需要重新计算.由于访问存储在WeakValueDictionary中的值之间没有强引用,因此GC消除了它们(即使内存完全没有问题).

现在,我如何使用弱参考资料来实现缓存?如果我明确地在某处保留强引用以防止GC删除我的弱引用,那么首先使用WeakValueDictionary是没有意义的.GC可能应该有一些选项告诉它:删除所有没有引用的内容以及只有在内存不足时(或超出某个阈值时)弱引用的所有内容.有类似的东西吗?或者这种缓存有更好的策略吗?

python caching garbage-collection weak-references

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

为什么 std::move 在这里生成指令?

我一次又一次听到这std::move(t)或多或少只是一种奇特的说法static_cast<T&&>(t),不会产生任何指示。当我现在std::move为了更好地理解移动语义而使用 godbolt 时,我发现它确实(或至少可能)生成指令。在这个例子中

#include <iostream>
using namespace std;

struct S {
    S() { cout << "default ctor" << endl; }
    S(S&& s) {
        i = s.i;
        s.i = 0;
        cout << "move ctor" << endl;
    }
    int i;
};

void foo(S s) { cout << "Foo called with " << s.i << endl; }

int main() {
    S s;
    foo(static_cast<S&&>(s));
    foo(std::move(s));
}
Run Code Online (Sandbox Code Playgroud)

foo导致以下汇编输出的调用

        ; ... snip ...
        lea     rdi, [rbp - …
Run Code Online (Sandbox Code Playgroud)

c++ move std c++11

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