小编usm*_*man的帖子

C++ 0x可变参数模板通过引用传递

我想为我的应用程序使用可变参数模板功能,但我不希望通过值传递对象(因为对象在我的情况下非常复杂).我想通过引用传递它们(而不是作为指针).

void func()
{
}

template<typename Function1, typename... FurtherFunctions>
void func(Function1 f1, FurtherFunctions... further_functions)
{
    // doing some processing here...
}
Run Code Online (Sandbox Code Playgroud)

我如何通过引用传递参数并确保它们不被复制?

c++ templates c++11

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

C++自动检测模板参数?

我试图定义一个像任务农业一样的递归结构.在这里,我正在尝试两个操作数,递归地可以为任意数量的操作数工作,因为它可以嵌套自己.

template <typename T1, typename T2>
class Farm
{
  private:
    T1 *task1;
    T2 *task2;
  public:
    // save them so that I can use them when invoking call operator
    Farm(T1 *_t1, T2 *_t2): task1(_t1), task2(_t2) { }

    void operator()()
    {
      // invoke call operator, meaning a farm could be a task (arbitrary nesting)
      (*task1)();
      (*task2)();
    }
};
int main()
{
    ... create two pointer(A *a, B *b...)
    Farm(a,b); // error: missing template arguments before ‘(’ token

    Farm<A, B>(a,b); // in this …
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

ATLAS gemm将未定义的引用链接到'cblas_sgemm'

这是我第一次尝试使用ATLAS.我无法正确链接.这是一个非常简单的sgemm程序:

...
#include <cblas.h>


const int M=10;
const int N=8;
const int K=5;

int main()
{
    float *A = new float[M*K];
    float *B = new float[K*N];
    float *C = new float[M*N];

    // Initialize A and B

    cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, 1.0, A, K, B, N, 0.0, C, N);

        ...
}
Run Code Online (Sandbox Code Playgroud)

当我在标准ATLAS安装的linux平台上编译它时,它会给出链接错误:

g++ test.c -lblas -lcblas -latlas -llapack
/tmp/cc1Gu7sr.o: In function `main':
test.c:(.text+0x29e): undefined reference to `cblas_sgemm(CBLAS_ORDER, CBLAS_TRANSPOSE, CBLAS_TRANSPOSE, int, int, int, float, float const*, int, float const*, int, …
Run Code Online (Sandbox Code Playgroud)

c c++ blas atlas

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

计算代码的触发器!

这真的花了我的时间.我找不到一种简单的方法来估计下面代码(循环)的FLOPS,循环的单次迭代有多少FLOPS:

float func(float * atominfo, float energygridItem, int xindex, int yindex)
{
   ...
   for (atomid=0; atomid<numatoms*4; atomid+=4) 
   {
       float dy = coory - atominfo[atomid+2];
       float dysqpdzsq = (dy * dy) + atominfo[atomid+3];
       float dx1 = coorx1 - atominfo[atomid+1];

       float s, y, t;
       s = atominfo[atomid] * (1.0f / sqrtf(dx1*dx1 + dysqpdzsq));
       y = s - energycomp1;
       t = energyvalx1 + y;
       energycomp1 = (t - energyvalx1)  - y;
       energyvalx1 = t;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

它看起来很简单,但我对之前给出的其他一些数字感到困惑,所以如果有人能给出一个确切的数字会很棒.

谢谢.

c c++ flops

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

GoogleTest如何在WithArg中使用InvokeArgument

我有一个模拟功能:

MOCK_METHOD4(my_func, int(double, double, void* (*cb) (int), int p1));
Run Code Online (Sandbox Code Playgroud)

我想以第三个参数作为参数调用上述函数的第二个(基于0的)参数,即,以“ p1”作为参数调用“ cb”函数。我怎样才能做到这一点?

我可以使用InvokeArgument调用带有某些自定义值的“ cb”:

ON_CALL(mockObj, my_func(_, _, _, _)).
                WillByDefault(DoAll(
                        IgnoreResult(InvokeArgument<2>(10)),
                        Return(0)));
Run Code Online (Sandbox Code Playgroud)

但是我想使用传递给相同的模拟函数调用的实际参数来调用它。

c++ googletest googlemock

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

MPI 发送和接收问题

我对 MPI 发送和接收操作有疑问。

假设我们有 2 个 MPI 线程尝试互相发送消息。以下是执行此操作的三个代码片段:

首先(阻止“发送”和“接收”):

...
int data = ...;
...
MPI_Send( &data, sizeof( int ), MPI_INT, 
        (my_id == 0)?1:0, 0, MPI_COMM_WORLD );

MPI_Status status; 
MPI_Recv( &data, sizeof( int ), MPI_INT,
        (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &status );
...
Run Code Online (Sandbox Code Playgroud)

第二(非阻塞“发送”但阻塞“接收”):

...
int data = ...;
...
MPI_Request request;
MPI_Isend( &data, sizeof( int ), MPI_INT, 
        (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &request);

MPI_Status status; 
MPI_Recv( &data, sizeof( int ), MPI_INT,
        (my_id == 0)?1:0, 0, MPI_COMM_WORLD, &status );
// …
Run Code Online (Sandbox Code Playgroud)

c mpi

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

在C++中以"对数"距离生成整数范围内的"N"个数字集

我找不到这个问题的确切答案,所以我在这里发布:如果我有一个整数范围,我想在这个范围内以相等的对数距离计算"N"数.

下面是一个示例代码,用于查找等于"非对数"距离(或多或少)的数字:

const int N = 100; // total no of sizes to generate
const int LOW = 10; // range low-bound
const int HIGH = 1000000; // range upper-bound
int SIZES[N];
int GAP = (HIGH-LOW)/N;
SIZES[0] = LOW + GAP;
for(int i=1; i<N; ++i)
{
   SIZES[i] = SIZES[i-1] + GAP;
}
Run Code Online (Sandbox Code Playgroud)

但是,我想在"对数"距离处找到此范围内的"N"个数字.

c c++ arrays algorithm

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

使用 GNU AutoTools 设置和使用数据目录路径

我正在尝试将 GNU AutoTools 用于我的 C++ 项目。我编写了configure.ac、makefile.am等。我有一些程序在执行过程中使用的文件,例如模板文件、XML模式等。因此,我沿着可执行文件安装/复制这些文件,为此我使用类似的文件:

abcdir = $(bindir)/../data/abc/
abc_DATA = ../data/knowledge/abc.cc
Run Code Online (Sandbox Code Playgroud)

现在它正确复制了文件,我的程序安装结构如下所示:

<installation_dir>/bin/<executableFile>
<installation_dir>/data/abc/abc.cc
Run Code Online (Sandbox Code Playgroud)

现在的问题是,在源代码中我实际上使用了这些文件(abc.cc 等),为此我需要这些文件所在的路径来打开它们。一种解决方案是定义(使用 AC_DEFINE)一些变量,例如 _ABC_PATH_ ,它指向安装路径,但究竟如何做到这一点?或者有没有更好的方法可以做到这一点。例如,在源代码中,我做了类似的事情:

...
ifstream input(<path-to-abc-folder> + "abc.cc"); // how to find <path-to-abc-folder>?
..
Run Code Online (Sandbox Code Playgroud)

c++ automake autoconf gnu autotools

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

C++模板元编程静态类型检查

我无法找到问题的答案,所以我将其作为一个问题发布.我做了一个小例子来解释它:

enum STORAGE_TYPE
{
    CONTIGUOUS,
    NON_CONTIGUOUS
};

template <typename T, STORAGE_TYPE type=CONTIGUOUS>
class Data
{
    public:
        void a() { return 1; }
};

// partial type specialization
template <typename T>
class Data<T, NON_CONTIGUOUS>
{
    public:
        void b() { return 0; }
};

// this method should accept any Data including specializations…
template <typename T, STORAGE_TYPE type>
void func(Data<T, type> &d)
{
    /* How could I determine statically the STORAGE_TYPE? */
    #if .. ?? 
        d.a();
    #else
        d.b();
    #endif      
}


int main() …
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

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

MPI阻塞接收语义

在阅读关于在MPI中阻止发送和接收时,我感到困惑.如MPI标准2.2第3.4节通信模式中所述:

...最后一节中描述的接收操作是阻塞:它仅在接收缓冲区包含新接收的消息后返回.接收可以在匹配发送完成之前完成(当然,只有在匹配发送开始后才能完成).

有人可以向我解释阻塞接收在相应的发送完成之前是如何完成的吗?我的理解是MPI_recv当数据准备好在接收缓冲区中使用时(即数据已被完全接收),阻塞的receive()返回.这不是这种情况吗?

compiler-construction mpi recv

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

C++模板对类的部分特化

我正在尝试使用类的C++部分模板专业化.问题更多的是语法而不是语义.其实我想要有以下内容:

int main()
{
   ...
   Reduce<int, float> r(new int, new float);
   Reduce<int> r2(new int); // partial template specialization?
   ...
}
Run Code Online (Sandbox Code Playgroud)

为了达到上述目的我试过:

template <typename T, typename U>
class Reduce {
  public:
    Reduce(T *t, U *u) { }
};

template <typename T>
class Reduce<T,T> {
  public:
    Reduce(T *t) { }
};
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,我不能使用以下语句:

Reduce<int> r2(new int); // error: wrong number of template arguments (1, should be 2)
Run Code Online (Sandbox Code Playgroud)

我还是要这样做:

Reduce<int, int> r2(new int); 
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下:(1)我怎样才能达到我想要的语法(如果可能的话)(2)如果不可能,为什么?(即技术问题)

c++ templates template-specialization

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

将新数据传递给可能仍在使用旧数据的异步线程函数

我遇到了一些与C/C++相关的问题:假设我有一些课程

class Demo
{
   int constant;
   public:
    void setConstant(int value)
    {
        constant=value;
    }
    void submitTask()
    {
       // need to make a call to C-based runtime system to submit a 
       // task which will be   executed "asynchronously"
       submitTask((void *)&constant);
    }
};

// runtime system will call this method when task will be executed
void func(void *arg)
{
    int constant= *((int *)arg);
    // Read this constant value but don't modify here....
}
Run Code Online (Sandbox Code Playgroud)

现在在我的应用程序中,我做了这样的事情:

int main()
{
  ...
  Demo objDemo;
  for(...)
  {
     objDemo.setConstant(<somevalue>); …
Run Code Online (Sandbox Code Playgroud)

c c++ multithreading asynchronous class

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