C++ 使用 GLuint 而不是 unsigned int 有什么好处?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8932912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:21:47  来源:igfitidea点击:

What's the advantage of using GLuint instead of unsigned int?

c++opengl

提问by JSeven

I like to be more standard as possible, so why should I "constrain" my classes defining it's members as OpenGL types when I can use primitive types? Is there any advantage?

我喜欢尽可能更标准,那么当我可以使用原始类型时,为什么要“约束”我的类将其成员定义为 OpenGL 类型?有什么优势吗?

回答by mah

The type "unsigned int" has a different size depending on the platform you're building on. I expect this to normally be 32 bits, however it could be 16 or 64 (or something else -- depending on the platform).

“unsigned int”类型的大小取决于您构建的平台。我希望这通常是 32 位,但也可能是 16 位或 64 位(或其他 - 取决于平台)。

Library-specific types are often created to be typedef'd according to platform-specific rules. This allows a generic application to use the right type without having to be aware of the platform it will be built for. Instead, the platform-specific knowledge is constrained to a single common header file.

特定于库的类型通常被创建为根据特定于平台的规则进行 typedef。这允许通用应用程序使用正确的类型,而不必知道将为其构建的平台。相反,特定于平台的知识仅限于单个公共头文件。

回答by JSeven

i don't think it matters in this case because the spec says they are minimum sizes, not strict sizes. have a look at gl.h ~line 149 they're just typedefs of basic C types. they are just a convenience - for example there is a boolean type, so if you're using C89 and don't use any booleans then there's one set up for you to use with GL. GLuint is just a shorter way of typing unsigned int:

我认为在这种情况下这并不重要,因为规范说它们是最小尺寸,而不是严格尺寸。看看 gl.h ~line 149 它们只是基本 C 类型的 typedef。它们只是一种方便 - 例如有一个布尔类型,所以如果您使用 C89 并且不使用任何布尔值,那么有一个设置供您与 GL 一起使用。GLuint 只是输入 unsigned int 的一种更短的方式:

typedef unsigned int  GLenum;
typedef unsigned char GLboolean;
typedef unsigned int  GLbitfield;
typedef void    GLvoid;
typedef signed char GLbyte;   /* 1-byte signed */
typedef short   GLshort;  /* 2-byte signed */
typedef int   GLint;    /* 4-byte signed */
typedef unsigned char GLubyte;  /* 1-byte unsigned */
typedef unsigned short  GLushort; /* 2-byte unsigned */
typedef unsigned int  GLuint;   /* 4-byte unsigned */
typedef int   GLsizei;  /* 4-byte signed */
typedef float   GLfloat;  /* single precision float */
typedef float   GLclampf; /* single precision float in [0,1] */
typedef double    GLdouble; /* double precision float */
typedef double    GLclampd; /* double precision float in [0,1] */

回答by karlphillip

Better cross-platform compatibility.

更好的跨平台兼容性。

回答by user877329

The advantages has already been mentioned here. However, there is a disadvantage clear from the following examples:

这里已经提到了优点。但是,从以下示例中可以明显看出缺点:

class FileIn
    {
    public:
        //Public interface like read
    private:
        void* handle;
    };

The above code fits very well in a platform independent header but writing

上面的代码非常适合独立于平台的标头,但编写

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

class FileIn
    {
    public:
        //Public interface like read
    private:
        HANDLE handle;
    };

does not.

才不是。

Though the former will require ugly typecasts like

虽然前者需要丑陋的类型转换,如

int fd=(int)( (size_t)handle );
close(fd);

i do not know any system which have sizeof(void*)< sizeof(int). Yes it will fail if open returns a negative number for a valid file handle.

我不知道任何具有sizeof(void*)< 的系统sizeof(int)。是的,如果 open 返回有效文件句柄的负数,它将失败。

What to learn about this? Avoid using typedefs in library include files. Instead use struct declarations even though C programmers need to write structa dozen times. Here, some C standard library implementations do it all wrong.

关于这个要学习什么?避免在库包含文件中使用 typedef。即使 C 程序员需要编写struct十几次,也可以使用结构声明。在这里,一些 C 标准库实现都做错了。

Right

In stdio.h:

stdio.h

struct FILE;

And in the application:

在应用程序中:

struct FILE* the_file=fopen("filename.txt","rb");
/*...*/

Wrong

错误的

In stdio.h:

stdio.h

typedef struct SOMENAMETHATNOONESHOULDUSE
    {
    /* Internal data members */
    } FILE;

In application

申请中

FILE* the_file=fopen("filename.txt","rb");

When writing a C++ wrapper, this forces either #include <cstdio>or simply declare the handle as above.

在编写 C++ 包装器时,这会强制#include <cstdio>或简单地声明句柄,如上。