我正在玩一些使用c ++ 11的玩具代码来弄清楚事情是如何运作的.在此期间,我遇到了以下问题,简化为:
template <int x, int y>
class add {
public:
static constexpr int ret = x + y;
};
constexpr int addFunc(const int x, const int y) {
return add<x,y>::ret;
}
int main() {
const int x = 1;
const int y = 2;
cout << add<x,y>::ret << endl; // Works
cout << addFunc(1,2) << endl; // Compiler error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用GCC 4.8.1并且输出为:
'x'不是模板参数中的常量表达式,用于'int'类型
'y'不是类型'int'的模板参数中的常量表达式
我试图计算的两种方式之间究竟有什么区别add::ret?这两个值都应该在编译时可用.
我正在研究多线程图像处理应用程序.我添加了一个基于QT的GUI,用于更改一些参数,我必须经常尝试,而不是每次启动程序时都从设置文件中加载它们或者必须输入它们.我也想要GUI显示每个线程的一些基本信息,以便我可以监控它们.我现在有通过设置在图像处理线程之间信息的线程安全的方法,我想办法查询一些从QT线程大约每秒此信息,以便我可以显示在用户界面上的一些反馈.
我的要求是我不想将QT特定代码合并到图像处理线程中以更新UI.我宁愿让UI线程轮询我当前用来在线程之间传递信息的方法.我希望我的代码库的图像处理部分独立,而不必依赖QT来运行.如何轮询全局可用功能以更新QT UI?
我使用OpenMP和MPI来并行化c中的一些矩阵运算.在矩阵上运行的一些函数是用Fortran编写的.Fortran函数需要传递一个缓冲区数组,该缓冲区数组仅在函数内部使用.目前我在每个并行部分分配缓冲区,类似于下面的代码.
int i = 0;
int n = 1024; // Actually this is read from command line
double **a = createNbyNMat(n);
#pragma omp parallel
{
double *buf;
buf = malloc(sizeof(double)*n);
#pragma omp for
for (i=0; i < n; i++)
{
fortranFunc1_(a[i], &n, buf);
}
free(z);
}
// Serial code and moving data around in the matrix a using MPI
#pragma omp parallel
{
double *buf;
buf = malloc(sizeof(double)*n);
#pragma omp for
for (i=0; i < n; i++)
{
fortranFunc2_(a[i], &n, …Run Code Online (Sandbox Code Playgroud) 我要知道是否可以在一个方法中使用if语句来检查所用泛型的类型.在让我思考这个问题的情况下,我希望以不同的方式处理一个非常基本的Point2D类和一个Point3D类.在我需要访问的3D点的情况下Point3d.z,我不确定这是否会导致问题我想做的伪代码版本是
public <T> void processPoints(T point) {
process(point.x);
process(point.y);
if (T == Point3D) { // What do I do here?
process(point.z); // Will accessing z cause problems?
}
}
Run Code Online (Sandbox Code Playgroud)
实际上,代码process表示更复杂并且z依赖于x,y因此我正在寻找避免代码重复的方法.我可能会想出一种方法来重载函数,但我很好奇所以我可以学习更多关于泛型的知识.