小编Ins*_*oop的帖子

std::forward_as_tuple 将参数传递给 2 个构造函数

我想传递多个参数以便在函数内构造两个对象,同样的方式也std::pair<T1, T2>(std::piecewise_construct, ...)可以。

所以我写了

template <typename Args0..., typename Args1...>
void f(std::tuple<Arg0> args0, std::tuple<Args1> args1) {
   Object0 alpha(...);
   Object1 beta(...);
   ...
}
Run Code Online (Sandbox Code Playgroud)

这样我就可以打电话

f(std::forward_as_tuple(..., ..., ...), std::forward_as_tuple(..., ...))
Run Code Online (Sandbox Code Playgroud)

但我不知道如何构建Object0Object1。我检查了我的标准库的源代码std::pair,他们似乎使用复杂的内部函数来获取 args0 和 args1 的索引。您知道如何做到这一点吗?

c++ tuples c++11

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

链接到不带 .lib 的 .dll 文件

我需要将一些 Delphi 代码重写为 C++,并且我们需要链接到动态库TMLComm2004.dll。事实证明,我们没有任何.lib文件,因此我们决定使用以下命令行生成它:

dumpbin /EXPORTS C:\Users\fayard\Desktop\TMLComm2004.dll > C:\Users\fayard\Desktop\TMLComm2004.txt
Run Code Online (Sandbox Code Playgroud)

我们得到如下文件

Microsoft (R) COFF/PE Dumper Version 14.00.24215.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file C:\Users\fayard\Desktop\TMLComm2004.dll

File Type: DLL

  Section contains the following exports for TMLcomm.dll

    00000000 characteristics
    401F6AD5 time date stamp Tue Feb  3 10:33:09 2004
        0.00 version
          1 ordinal base
          27 number of functions
          27 number of names

    ordinal hint RVA      name

          1    0 00001122 _MSK_COFFDownloadFlash@16
          2    1 0000114F _MSK_COFFDownloadRAM@20
          3    2 0000106E …
Run Code Online (Sandbox Code Playgroud)

c dll name-mangling

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

连接一个字符串系列

我想编写一个函数,它连接场景后面std::string只有一个malloc的任何序列.因此,需要首先计算字符串的总长度.该函数需要以这种方式使用:

std::string s0 = ...;
std::string s1 = ...;
std::string s2 = ...;

std::string s = join(s0, s1, s2);
Run Code Online (Sandbox Code Playgroud)

一个更好的join将使用的混合std::stringstd::string_view.如果我们可以添加字符串文字会更好.你会如何在C++ 11中编写这样的函数(它需要用gcc 4.8.5和编译Visual Studio 2015)?

c++ string string-concatenation

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

用于小型未对齐数据的快速 memcpy

我需要读取一个由许多基本类型组成的二进制文件,例如 int、double、UTF8 字符串等。例如,考虑一个文件包含 n 对 (int, double) 一个接一个,没有任何对齐n 在数千万的数量级。我需要非常快速地访问该文件。我使用fread调用和我自己的大约 16 kB 长的缓冲区读取文件。

分析器显示我的主要瓶颈恰好是从内存缓冲区复制到其最终目的地。编写从缓冲区复制到双精度的函数的最明显方法是:

// x: a pointer to the final destination of the data
// p: a pointer to the buffer used to read the file
//
void f0(double* x, const unsigned char* p) {
  unsigned char* q = reinterpret_cast<unsigned char*>(x);
  for (int i = 0; i < 8; ++i) {
    q[i] = p[i];
  }
}
Run Code Online (Sandbox Code Playgroud)

我使用以下代码,我在 x86-64 上获得了巨大的加速

void f1(double* x, const unsigned char* p) {
  double* …
Run Code Online (Sandbox Code Playgroud)

c++ memory performance

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

模板参数简化

我是C++的新手(使用C++ 2011),我想找到解决以下问题的方法.我有一个代表一个功能的类:

class Curve {
private:
    ...
public:
    std::array<double, 3> value(double s);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用该对象将此函数传递给由类表示的算法:

template <int n, typename Functor>
class Algorithm {
private:
    Functor f;
    std::array<double, n> a;
public:
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后我创建了对象

Algorithm<3, Curve> solver;
Run Code Online (Sandbox Code Playgroud)

但是3显然是来自任何类型Curve的方法值返回的数组大小的3.我想简化这段代码,以便我可以使用:

Algorithm<Curve> solver;
Run Code Online (Sandbox Code Playgroud)

但我不知道该怎么做.你介意给我一个提示吗?

最好的问候,弗朗索瓦

c++ templates

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

在Windows上创建缓慢的线程

我已经使用C++ 11工具将数字运算应用程序升级为多线程程序.它在Mac OS X上运行良好,但不受益于Windows上的多线程(Visual Studio 2013).使用以下玩具程序

#include <iostream>
#include <thread>

void t1(int& k) {
    k += 1;
};

void t2(int& k) {
    k += 1;
};

int main(int argc, const char *argv[])
{
    int a{ 0 };
    int b{ 0 };

    auto start_time = std::chrono::high_resolution_clock::now();
    for (int i = 0; i < 10000; ++i) {
        std::thread thread1{ t1, std::ref(a) };
        std::thread thread2{ t2, std::ref(b) };
        thread1.join();
        thread2.join();
    }
    auto end_time = std::chrono::high_resolution_clock::now();
    auto time_stack = std::chrono::duration_cast<std::chrono::microseconds>(
        end_time - start_time).count();
    std::cout …
Run Code Online (Sandbox Code Playgroud)

windows multithreading c++11

4
推荐指数
2
解决办法
1147
查看次数

根据模板参数删除成员

我想设计一个带矢量优化的矢量类.看起来像:

template <typename T, int small_size = 0>
class Vector {
private:
    T data_small_[small_size];
    T* data_;
    T* size_;
    T* capacity_;
public:
    ...
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,大多数情况下,该类将与small_size = 0一起使用.有没有办法删除data_small_ for small_size = 0而不进入模板专门化并重写该类的整个代码?

c++ templates vector

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

将long int*转换为long long int*

在我的平台上,long int和long long int都是相同的(64位).我有一个指向long int的指针,我想将它转换为指向long long int的指针.以下代码

static_cast<long long int*>(pointer);
Run Code Online (Sandbox Code Playgroud)

不被允许.在C++ 11中这样做的正确方法是什么?

c++ c++11

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

Std :: ptrdiff_t和std :: size_t的奇怪typedef

该类型std::size_t是一个无符号类型,可以存储任何类型的理论上可能的对象的最大大小,并且std::ptrdiff_t是一个可以保存两个指针差异的有符号类型(这是另一种说法应该是数组的标准整数的方式在C++中索引).C++标准库已决定std::size_t用于数组索引,但通常认为这std::ptrdiff_t是一个更好的选择.哪一个是最好的是一个长期的辩论,我不想进入这里,但我一直认为第二个是第一个的未签名版本.在macOS上运行此程序

#include <cstddef>
#include <cstdio>

void f(int n) { std::printf("int"); };
void f(long n) { std::printf("long"); };
void f(long long n) { std::printf("long long"); };
void f(unsigned int n) { std::printf("unsigned int"); };
void f(unsigned long n) { std::printf("unsigned long"); };
void f(unsigned long long n) { std::printf("unsigned long long"); };

int main() {
  const std::ptrdiff_t n_ptrdiff = 0;
  const std::size_t n_size = 0;

  std::printf("std::ptrdiff_t is an alias for ");
  f(n_ptrdiff);
  std::printf("\n"); …
Run Code Online (Sandbox Code Playgroud)

c++

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

在 C 中释放对象的设计模式

当用 C 语言编程时,我们通常创建初始化的数据结构,然后在不再需要时将其释放。例如,如果我们想创建一个动态的 double 数组,通常声明

struct vector {
    double *data;
    int size;
    int capacity;
}
typedef struct vector vector;

vector *v_new(int n) {
    vector *v = malloc(sizeof(vector));
    v->data = malloc(n * sizeof(double));
    v->size = n;
    v->capacity = n;
    return v;
}
Run Code Online (Sandbox Code Playgroud)

问题是关于自由函数的常见模式。在 C 语言中,该函数free接受 NULL 指针并且不执行任何操作。以这种方式设计v_free函数是一种常见的模式,还是它们通常期望一个非 NULL 指针?明确地说,您期望这种实现吗?

void v_free(vector *v) {
    if (v != NULL) {
        free(v->data);
    }
    free(v);
}
Run Code Online (Sandbox Code Playgroud)

或者这个?

void v_free(vector *v) {
    free(v->data);
    free(v);
}
Run Code Online (Sandbox Code Playgroud)

提出这个问题是因为我们在法国预科学校开始向本科生教授C语言,而我们在“C设计模式”方面没有那么多经验。

谢谢你的建议。

c

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