小编Aar*_*man的帖子

C++ 11,14或17是否提供了一种从decltype()中获取参数的方法?

这个问题非常类似于:" 从decltype(someFunction)中提取参数类型列表 ".我不确定那里的答案是否符合我的意图.我希望能够创建一个模板函数,该函数根据函数指针模板参数(whistles)的类型推断出其运行时参数的类型.

对于一个示例用例,假设我想使用加载了LD_PRELOAD的填充库来直接测量C POSIX文件I/O. 我可以为fopen,fread,fwrite,fclose编写单独的包装器......如果所有这些包装器都做类似的东西,如果我能定义一个捕获常见行为的模板会不会很好?

部分示例不使用演示涉及多少样板文件的模板:

extern "C" {

FILE *(*real_fopen)(const char *, const char *) = NULL;
FILE *fopen(const char *path, const char *mode)
{
    FILE *returned_file;

    if (real_fopen == NULL) {
        real_fopen = ((FILE *)(const char *, const char *))dlsym("fopen", RTLD_NEXT);
    }

    ... do pre-call instrumentation ...
    returned_file = real_fopen(path, mode);
    ... do post-call instrumentation ...

    return returned_file;
}

int (*real_fclose)(FILE *) = NULL;
int fclose(FILE *fp)
{
    int retval;

    if (real_fclose == NULL) { …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 argument-deduction

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

Python 3扼流CP-1252/ANSI读取

我正在研究一系列解析器,我从单元测试中得到一堆回溯,如:

  File "c:\Python31\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 112: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

使用open()打开文件,没有额外的arguemnts.我可以将额外的参数传递给open()或者在编解码器模块中使用某些东西以不同方式打开它们吗?

这提出了用Python 2编写并使用2to3工具转换为3的代码.

更新:事实证明这是将zipfile输入解析器的结果.单元测试实际上预计会发生这种情况.解析器应该将其识别为无法解析的内容.所以,我需要改变我的异常处理.在这样做的过程中.

python unicode cp1252 latin1 python-3.x

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

如何在Scala中调用树代数数据类型的叶子的构造函数?

我正在创建一些基本的抽象数据类型和算法,以便了解我的CS基础知识,并在此过程中学习Scala.我遇到了BinarySearchTree数据类型的问题,这是一个更抽象的BinaryTree的实现:

abstract class BinaryTree[T](stored_value: T) { 
  var contents = stored_value
  var l: this.type = _
  var r: this.type = _
  ...
}

class BinarySearchTree[T <: Ordered[T]](stored_value: T) extends BinaryTree(stored_value) {  
  def insert(newval: T) {
    if (newval <= contents) {
      if (l == null) {
        throw new NotDefinedError("Still trying to work around type erasure.")
      } else {
        l.insert(newval)
      }
    } else {
      if (r == null) {
        throw new NotDefinedError("Still trying to work around type erasure.")
      } else {
        r.insert(newval)
      } …
Run Code Online (Sandbox Code Playgroud)

generics constructor scala abstract-data-type algebraic-data-types

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