我试图使用fold表达式作为[]运算符的参数.不幸的是,只有第一个元素是正确的.
template <class ...U> T& operator[](U ...indices){
size_t i=0, k[get_n_dimensions()];
(... , void(k[i++]=indices));
// use k[0], k[1], ...
// only k[0] is correct
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我对函数的参数使用相同的语法,它可以正常工作.
template <class ...U> T get(U ...indices) const {
size_t i=0, k[get_n_dimensions()];
(... , void(k[i++]=indices));
// k[0], k[1], ... filled correctly
}
Run Code Online (Sandbox Code Playgroud)
是什么原因?什么是解决方案?
我在第二个cudaMalloc之后获得了一个分段错误.
#include <cuda.h>
#include <cuda_runtime.h>
int main(){
int n=16;
float2* a;
cudaMalloc((void **) a, n*sizeof(float2));
float2* b;
cudaMalloc((void **) b, n*sizeof(float2));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我注释掉2个cudaMalloc中的任何一个,那么代码运行正常.
谢谢!