小编Ada*_*dam的帖子

"extern C++"如何工作?

我跳了进去winnt.h,发现代码如下:

extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];
Run Code Online (Sandbox Code Playgroud)

我想问一下如下问题:

  1. extern "C++"工作怎么样?
  2. 这是GCC中的便携式和Clang吗?
  3. 是否可以使用此语法导出所有模板?

对于问题3,我的意思是我可以将模板的declearation和定义分开,然后为模板生成动态链接,而不使用这个技巧实际给出实现吗?

c++ templates extern

23
推荐指数
2
解决办法
1343
查看次数

有没有好的色彩映射使用python的PIL将灰度图像转换为彩色图像?

Matplotlib有很多很好的色彩图,但性能不好.我正在编写一些代码来使灰度图像变得鲜艳,其中使用颜色图进行插值是一个好主意.我想知道是否有开源彩色地图或演示代码使用Pillow通过colormap将灰度图像转换为彩色图像?


澄清:

  1. Matplotlib非常适合演示使用,但对于图像的thounsands表现不佳.
  2. Matplotlib色彩贴图
  3. 您可以将灰度图像映射到色彩图以获得彩色图像.

演示:

第一个图像是灰度,第二个图像是'jet'cmap,第三个是'热'.

Matplotlib演示

问题是我对颜色知之甚少,我想在PIL中实现这样的效果以获得更好的性能.

python colors matplotlib python-imaging-library

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

如何在Fish shell中检索当前脚本的路径

在bash中,我们可以通过$0变量检索当前脚本的路径,因此如果任何脚本具有与脚本目录位于同一目录下的依赖项资源,即使我们没有在脚本的目录中执行脚本,我们也可以使用它.

如何在fish shell中检索当前脚本的路径?

fish

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

有没有办法在鱼壳中回显/捕获多行消息?

在 Bash/Zsh 中,可以将 cat 用于多行消息,例如:

cat <<DELIM
This is line 1
This is line 2
DELIM
Run Code Online (Sandbox Code Playgroud)

但是,上述代码不适用于 Fish Shell。有没有办法这样做?

shell fish

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

通过引用传递与通过shared_ptr传递

Stackoverflow上的大多数问题都是关于shared_ptr应该通过ref或value传递.不过我的问题是这样的例子:

class Foo;

void function1(Foo & ff) { ff.m_abc = 1024; }
void function2(const std::shared_ptr<Foo> & ff) { ff->m_abc = 1024; }
Run Code Online (Sandbox Code Playgroud)

function1function2可以使用和改变FF的某些部分.


我的情况在这里:

我需要使用arg *this或者调用函数shared_from_this().

print(msg, *this);

or

print(msg, this->shared_from_this());
Run Code Online (Sandbox Code Playgroud)

我可以在我的代码中使用function1function2样式化一个函数.

但是,如果我使用function2样式,我需要实现Foo继承std::enable_shared_from_this,但是有了function1样式,我不需要.

我在单线程环境中使用此功能

c++ shared-ptr c++11

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

生命周期如何处理常量字符串/字符串文字?

在官方网站上阅读了教程,我对常量字符串/字符串文字的生命周期有一些疑问.

我编写以下代码时出错:

fn get_str() -> &str {
    "Hello World"
}
Run Code Online (Sandbox Code Playgroud)

错误:

error[E0106]: missing lifetime specifier
 --> src/main.rs:1:17
  |
1 | fn get_str() -> &str {
  |                 ^ expected lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
  = help: consider giving it a 'static lifetime
Run Code Online (Sandbox Code Playgroud)

但是添加参数时没关系:

fn get_str(s: &str) -> &str {
    "Hello World"
}
Run Code Online (Sandbox Code Playgroud)

为什么这样做?如何"Hello World"借用参数s,即使它与它无关 …

string lifetime string-literals rust

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

如何在C++的#define中扩展'#'?

我想在debuging时暴露私人成员,如下所示:

class A {
public:
  void f1();
#ifndef NDEBUG
public:
#else
private:
#endif
  void f2();
};
Run Code Online (Sandbox Code Playgroud)

我想使用像:

#define PUBLIC public:
#define PRIVATE \
\#ifndef NDEBUG \
public: \
\#else \
private: \
\#endif
Run Code Online (Sandbox Code Playgroud)

但是,我知道这不起作用......

你们推荐什么?Thx提前.


编辑01:

我的目的不是为了debug我的代码,而是为了测试私有成员函数.

c++

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

为什么ubuntu 12.04下的OpenMP比串行版慢

我已经阅读了关于这个主题的其他一些问题.但是,无论如何,他们并没有解决我的问题.

我编写的代码如下,我的pthread版本和omp版本都比串行版慢.我很困惑.

在环境下编译:

Ubuntu 12.04 64bit 3.2.0-60-generic
g++ (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1

CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Vendor ID:             AuthenticAMD
CPU family:            18
Model:                 1
Stepping:              0
CPU MHz:               800.000
BogoMIPS:              3593.36
L1d cache:             64K
L1i cache:             64K
L2 cache:              512K
NUMA node0 CPU(s):     0,1
Run Code Online (Sandbox Code Playgroud)

编译命令:

g++ -std=c++11 ./eg001.cpp -fopenmp

#include <cmath>
#include <cstdio>
#include <ctime>
#include <omp.h>
#include <pthread.h>

#define NUM_THREADS 5
const int sizen = 256000000;

struct Data {
    double …
Run Code Online (Sandbox Code Playgroud)

c++ ubuntu pthreads openmp

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

C++中的函数const本地对象会发生什么?

我想知道为什么会这样:接下来的两个例子提供了非常不同的结果,第一个实际上让我震惊.

例1:

#include <iostream>

class A
{
public:
    A() {
        for(int i = 0; i < 10000; ++i)
            for(int j = 0; j < 10000; ++j)
                m += i+j;
    }
    ~A() {}

    double m;
};

void foo()
{
    const A a;

    std::cout << a.m << std::endl;
}//foo

int main()
{
    for(int i = 0; i < 10; ++i)
        foo();

    return 0;
}//main
Run Code Online (Sandbox Code Playgroud)

结果:

9.999e+11
1.9998e+12
2.9997e+12
3.9996e+12
4.9995e+12
5.9994e+12
6.9993e+12
7.9992e+12
8.9991e+12
9.999e+12
Run Code Online (Sandbox Code Playgroud)

例2:

#include <iostream>

class A
{
public: …
Run Code Online (Sandbox Code Playgroud)

c++ initialization class

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

为什么OpenMP'simd'比'parallel for simd'具有更好的性能?

我正在使用英特尔编译器OpenMP 4.0开发Intel E5(6核,12个线程)

为什么这段代码SIMD-ed比并行SIMD-ed更快?

for (int suppv = 0; suppv < sSize; suppv++) {
  Value *gptr = &grid[gind];
  const Value * cptr = &C[cind];

  #pragma omp simd // vs. #pragma omp parallel for simd
  for (int suppu = 0; suppu < sSize; suppu++)
    gptr[suppu] += d * cptr[suppu];

  gind += gSize;
  cind += sSize;
}
Run Code Online (Sandbox Code Playgroud)

随着更多线程,它变得更慢.


编辑1:*grid是4096*4096矩阵,数据结构:vector<complex<double>> *C2112*129*129矩阵,数据结构:vector<complex<double>> *gSize = 4096*sSize = 129.

  • 编译器标志:icpc -march = native -std = c ++ 11 …

c++ concurrency performance openmp

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