EDIT:添加了 PlasmaHH 建议的带有内存位置的调试输出。
我不明白 OpenCL 的 C++ 绑定中 cl::vector 的不同行为。考虑以下代码:
Header Top.hpp
:
class Top {
public:
void setBool(bool b);
bool getBool();
private:
bool status;
};
Source Top.cpp
:
#include "Top.hpp"
void Top::setBool(bool b) {
std::cout << (void*)this << " setBool("<< b<< ")\n";
status = b;
}
bool Top::getBool() {
std::cout << (void*)this << " getBool() returns " << status << std::endl;
return status;
}
使用上面的:
#define __NO_STD_VECTOR
#include <iostream>
#include "CL/cl.hpp"
#include "Top.hpp"
using namespace cl;
using namespace std;
cl::vector<Top> js;
int main() {
js.push_back(Top());
js[0].setBool(true);
cout << js[0].getBool() << endl;
for(cl::vector<Top>::iterator i = js.begin(); i != js.end(); ++i) {
(*i).setBool(false);
}
cout << js[0].getBool() << endl;
}
With __NO_STD_VECTOR
std::vector 被覆盖。输出是
0x6021c0 setBool(1)
0x6021c0 getBool() returns 1
0x7fffae671d60 setBool(0)
0x6021c0 getBool() returns 1
所以迭代器返回的位置肯定是错误的。
将以上内容与std::vector
(并将命名空间更改为std
当然)但是给出了预期的输出:
0x1be0010 setBool(1)
0x1be0010 getBool() returns 1
0x1be0010 setBool(0)
0x1be0010 getBool() returns 0
这个迭代器的行为有所不同,但它应该替换 std::vector 以避免兼容性问题。我错过了什么吗?