How to cast an integer to a void* without violating C++ core guidelines?(如何在不违反 C++ 核心准则的情况下将整数转换为 void*?)
问题描述
这有效,但也会导致不要使用 reinterpret_cast (type.1)"警告:
This works but also results in "Don't use reinterpret_cast (type.1)" warning:
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8,
reinterpret_cast<void*>(sizeof(GLfloat) * 3));
这不会编译:
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8,
static_cast<void*>(sizeof(GLfloat) * 3));
这不会编译:
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8,
dynamic_cast<void*>(sizeof(GLfloat) * 3));
这显然有效,但在 C++ 中似乎是一个很大的禁忌(不要使用 C 风格的强制转换(type.4)")
This obviously works but seems to be a big no-no in C++ ("Don't use C-style casts (type.4)")
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8,
(void*)(sizeof(GLfloat) * 3));
我应该忽略关于 reinterpret_cast 的警告吗?
Should i just ignore the warning about the reinterpret_cast?
推荐答案
当您进行低级编程时,您偶尔会不得不做一些 C++ 核心指南规定您不应该做的事情.因此,只需执行这些操作,要么接受警告",要么关闭该特定指南(可能在每个文件的基础上).
When you're doing low-level programming, you will occasionally have to do things that the C++ core guidelines say you shouldn't do. So just do them and either live with the "warning" or turn off that specific guideline (possibly on a per-file basis).
话虽如此,需要这种特殊的低级欺骗完全是因为 OpenGL 在其顶点规范 API 中的愚蠢.该值应该是某种整数字节偏移量,而不是转换为另一方将转换回偏移量的指针的偏移量.
That having been said, the need for this particular bit of low-level fudgery is solely because of OpenGL's stupidity in its vertex specification API. That value ought to be an integer byte offset of some sort, not an offset cast to a pointer which the other side will cast back to an offset.
所以最好完全避免使用糟糕的 API.使用单独的属性格式规范,而不是旧式的glVertexAttribPointer.它几乎在各个方面都优越.它会将您的代码从:
So it would be better to just avoid the bad API altogether. Use separate attribute format specification rather than the old-style glVertexAttribPointer. It's superior in pretty much every way. It will turn your code from:
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 8,
reinterpret_cast<void*>(sizeof(GLfloat) * 3));
到
glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 3); //Offset specified as integer.
glVertexAttribBinding(1, 0); //You seem to be using multiple attributes with a stride, so they should use the same buffer binding.
//Some later point when you're ready to provide a buffer.
glBindVertexBuffer(0, buffer_obj, 0, sizeof(GLfloat) * 8); //Stride goes into the buffer binding.
看,根本没有演员.
遗憾的是,glDrawElements 函数系列没有替代方案,因此您仍会收到此警告.
Unfortunately, there are no alternatives for the glDrawElements family of functions, so you're still going to get this warning.
这篇关于如何在不违反 C++ 核心准则的情况下将整数转换为 void*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不违反 C++ 核心准则的情况下将整数转换为 void*?
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
