如何在不违反C ++核心准则的情况下将整数转换为void *? [英] How to cast an integer to a void* without violating C++ core guidelines?

查看:151
本文介绍了如何在不违反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样式的强制类型转换(类型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 *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆