错误:在类型 "blah blah" 的绑定引用中删除限定符以初始化"some other blah blah"
Error: qualifiers dropped in binding reference of type "blah blah" to initialize "some other blah blah"
所以,当我在线程外创建"boxes"answers"boxbound"变量时,我会遇到运行时错误,但当我将其移动到线程内的for循环中时,错误就会消失,原因是什么?
void Flyscene::raytraceScene(int width, int height) {
std::cout << "ray tracing ..." << std::endl;
//start of acceleration structure
std::vector<std::vector<Tucano::Face>> boxes = firstBox(mesh);
std::vector<std::vector<Eigen::Vector3f>> boxbounds;
for (int i = 0; i < boxes.size(); i++) {
boxbounds.push_back(getBoxLimits(boxes[i], mesh));
}
/////
// if no width or height passed, use dimensions of current viewport
Eigen::Vector2i image_size(width, height);
if (width == 0 || height == 0) {
image_size = flycamera.getViewportSize();
}
// create 2d vector to hold pixel colors and resize to match image size
vector<vector<Eigen::Vector3f>> pixel_data;
pixel_data.resize(image_size[1]);
for (int i = 0; i < image_size[1]; ++i)
pixel_data[i].resize(image_size[0]);
// origin of the ray is always the camera center
Eigen::Vector3f origin = flycamera.getCenter();
Eigen::Vector3f screen_coords;
// Multi Threading
// Comment this if you don't want multi-threading
//-----------------------------------------------------//
int max_pixels = (image_size[0] * image_size[1]); //width * height
// Get amount of cores of your CPU
int cores = std::thread::hardware_concurrency();
// Keep track of # of pixels (atomic making sure no 2 threads render the same pixel)
volatile std::atomic<std::size_t> curr_pixel(0);
// Stores all cores assigned to a task
std::vector<std::future<void>> future_vector;
cout << "Threads supported: " << cores << "n";
while (cores--)
future_vector.emplace_back(
std::async([=, &origin, &curr_pixel, &pixel_data]()
{
while (true)
{
int index = curr_pixel++;
if (index >= max_pixels)
break;
std::size_t i = index % image_size[1];
std::size_t j = index / image_size[1];
//cout << "at index: " << index << std::endl;
// create a ray from the camera passing through the pixel (i,j)
auto screen_coords = flycamera.screenToWorld(Eigen::Vector2f(i, j));
// launch raytracing for the given ray and write result to pixel data
pixel_data[i][j] = traceRay(0,origin, screen_coords, boxes, boxbounds);
if (index % 10000 == 0) {
std::cout << "Percentage done (mt): " << (float)(index / 10000) << "%" << std::endl;
}
}
}));
// Call futures (Async jobs), this will activate all process on the cores
for (auto& e : future_vector) {
e.get();
}
然而,当我把它像下面一样移到里面时,错误就会消失;
void Flyscene::raytraceScene(int width, int height) {
std::cout << "ray tracing ..." << std::endl;
// if no width or height passed, use dimensions of current viewport
Eigen::Vector2i image_size(width, height);
if (width == 0 || height == 0) {
image_size = flycamera.getViewportSize();
}
// create 2d vector to hold pixel colors and resize to match image size
vector<vector<Eigen::Vector3f>> pixel_data;
pixel_data.resize(image_size[1]);
for (int i = 0; i < image_size[1]; ++i)
pixel_data[i].resize(image_size[0]);
// origin of the ray is always the camera center
Eigen::Vector3f origin = flycamera.getCenter();
Eigen::Vector3f screen_coords;
// Multi Threading
// Comment this if you don't want multi-threading
//-----------------------------------------------------//
int max_pixels = (image_size[0] * image_size[1]); //width * height
// Get amount of cores of your CPU
int cores = std::thread::hardware_concurrency();
// Keep track of # of pixels (atomic making sure no 2 threads render the same pixel)
volatile std::atomic<std::size_t> curr_pixel(0);
// Stores all cores assigned to a task
std::vector<std::future<void>> future_vector;
cout << "Threads supported: " << cores << "n";
while (cores--)
future_vector.emplace_back(
std::async([=, &origin, &curr_pixel, &pixel_data]()
{
while (true)
{
int index = curr_pixel++;
if (index >= max_pixels)
break;
std::size_t i = index % image_size[1];
std::size_t j = index / image_size[1];
//cout << "at index: " << index << std::endl;
//start of acceleration structure
std::vector<std::vector<Tucano::Face>> boxes = firstBox(mesh);
std::vector<std::vector<Eigen::Vector3f>> boxbounds;
for (int i = 0; i < boxes.size(); i++) {
boxbounds.push_back(getBoxLimits(boxes[i], mesh));
}
/////
// create a ray from the camera passing through the pixel (i,j)
auto screen_coords = flycamera.screenToWorld(Eigen::Vector2f(i, j));
// launch raytracing for the given ray and write result to pixel data
pixel_data[i][j] = traceRay(0,origin, screen_coords, boxes, boxbounds);
if (index % 10000 == 0) {
std::cout << "Percentage done (mt): " << (float)(index / 10000) << "%" << std::endl;
}
}
}));
// Call futures (Async jobs), this will activate all process on the cores
for (auto& e : future_vector) {
e.get();
}
这里还有rayTrace方法:
Eigen::Vector3f Flyscene::traceRay(int level, Eigen::Vector3f& origin, Eigen::Vector3f& dest, std::vector<std::vector<Tucano::Face>>& boxes, std::vector<std::vector<Eigen::Vector3f>>& boxbounds)
你认为这是为什么?
以下是完整的错误描述:
错误(活动(E0433限定符在类型为"std::vector>,std::allocater>>&"的绑定引用中被丢弃到类型为"const std::vector>,std::allocater>>"的初始值设定项光线跟踪
类型的绑定引用中丢弃了错误(活动(E0433限定符"std::vector>,std::分配器>>&"到类型为"const std::vector>,std::分配器>>"的初始值设定项光线跟踪
您需要将mutable
添加到lambda中。
矢量是通过引用(传递给traceRay
(的,因此可以在该函数中对其进行修改。您的lambda通过复制=
(用于捕获(获取向量,=
捕获的对象只能读取,您不能修改它们。
您的代码可以简化为以下示例:
void bar(std::vector<int>& v) {
}
void foo() {
std::vector<int> v;
auto l = [=]() /*mutable*/
{
bar(v); // works only with uncommented mutable
// v can be modified only with mutable
};
l();
}
在lambda中创建向量时,不会捕获它们,因此可以在traceRay
中更改它们。
所以在第一个片段中添加mutable
:
std::async([=, &origin, &curr_pixel, &pixel_data]() mutable
{ ^^^^^^^
while (true)
{
相关文章:
- "std::list::splice(std::const_iterator pos, std::list&& other)"是否保证将"其他"留空?
- 终端如何在 shell 提示符下以编程方式阻止退格,以便您无法在其上退格,例如 $ 表示 bash 或 C:\blah\>
- 错误:在类型 "blah blah" 的绑定引用中删除限定符以初始化"some other blah blah"
- 为什么'allocate in one library and free in the other'是错误的
- 不可变的全局对象应该声明为"const my_result_t BLAH"还是"extern const my_result_t BLAH;"?
- "How to use long long data-type rather than pointers data-type to modify other variables ?"
- makefile c++ with other h file dependencies with OpenGL
- 是否必须在内存中"next to each other"两个相邻声明的公共字段?
- forward_list::splice_after( const_iterator pos, forward_list&other, const_iterator i ) 功能
- Nsight or other Direct3D debbuger/performance analyser for V
- 在磁贴上显示 V2 凭据提供程序'Other user'
- 在Linux中查找任务栏的大小(C/C++/gtkm/other)
- 错误:令牌之前的预期主表达式'OTHER'
- C++ DLL 到 C# 错误:"Attempted to read or write protected memory. This is often an indication that other
- 在 C/C++ 中子集'every other bits'的最简单方法是什么?
- 如何声明具有other类型成员的两个结构体
- 'std::map<..> a; blah = a[abcd];' 线程是否安全,如果在此调用之前未创建 ABCD?
- gcc-gprof/gcov/other-如何获取函数调用/出口+控制流语句的序列
- 如何将 Doxygen 的 "Documentation at other places" 功能与专门的模板类一起使用?
- QTreeView, QItemDelegate & refresh other item?