1
$\begingroup$

gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)

I have never seen this syntax before. Presumably glfGetProcAddres is a function, but it has the brackets behind it. Is this only possible in open gl or is my c++ trash.

$\endgroup$
4
  • $\begingroup$ "it has the brackets behind it" Do you mean in front of it? $\endgroup$ Feb 26, 2019 at 18:40
  • $\begingroup$ You are right. I have it reversed for some reason. Can youbtell me $\endgroup$
    – Fare Osoba
    Feb 26, 2019 at 18:42
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because this is a fundamental question about the C++ programming language entirely unrelated to the problem of computer graphics. $\endgroup$ Feb 27, 2019 at 18:50
  • $\begingroup$ I will delete it sorry. I didn't mean to piss anybody off, im just a newbie, the negative votes are a bit harsh :( $\endgroup$
    – Fare Osoba
    Feb 27, 2019 at 18:56

1 Answer 1

5
$\begingroup$

The signature of glfwGetProcAddress is:

GLFWglproc glfwGetProcAddress(const char *);

It's a function that takes a string and returns a GLFWglproc. which is defined as

typedef void(* GLFWglproc) (void);

Essentially, that means glfwGetProcAddress is a function that takes a string and returns a pointer to function with no parameters and no return value.


GLADloadproc is a type alias defined as follows:

typedef void* (* GLADloadproc)(const char *name);

Essentially, this means that this is the type of a function that takes a string and returns a void pointer (this is a type of pointer that can be safely converted to from every other type of pointer).


in the expression

gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)

Inside, there is the expression

(GLADloadproc)glfwGetProcAddress

Where we have a type in parenthesis followed by an identifier. This is known as a type cast, it means we are taking some value of some type and converting it to some other type.

In this case, we take our GLADloadproc function that takes string and returns pointer to function and we 'disguise' it as a function that takes string and returns void*. This works because we can convert any pointer to void*, even in function pointer return types.

This is all to end up passing it to gladLoadGLLoader, which expects a value of type GLADloadproc.


TL;DR

gladLoadGLLoader takes a GLADloadproc but we want to give it a GLFWglproc (*)(const char *). To be able to do this, we force the type system to take it anyways by using a type cast.

$\endgroup$
2
  • $\begingroup$ Great answer. You look young, how are you so smart :P $\endgroup$
    – Fare Osoba
    Feb 26, 2019 at 20:14
  • $\begingroup$ And the reason you pass this function pointer to the windowing system is so that the windowing system can use this "OpenGL extension loader mechanism" for loading functions... $\endgroup$
    – jackw11111
    Aug 7, 2020 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.