use unique_ptr with GLFWwindow
Asked Answered
F

2

9

I am using GLFW for handling window events in an application. It works fine. Later I decide to remove raw pointers starting from GLFWwindow. It defined in the file glfw3.h as:

typedef struct GLFWwindow GLFWwindow; 

And I cannot find the actual definition of the structure in the header file. So I assume this is a kind of forward declaration, but I have no idea why it is not like

struct GLWwindow;

I tried to use the later form of forward declaration to replace the former form. It compiles fine. What is the pro of the former formation of forward declaration?

So the real questions, since the structure GLFWwindow is only a declaration, unique pointer can not complete template specialization without definition. I can not use unique_ptr to declare any pointer. The compiler gives me error

C2027 use of undefined type 'GLFWwindow'
C2338 can't delete an incomplete type
C4150 deletion of pointer to incomplete type 'GLFWwindow';no destructor called

Anyone knows how to use unique pointer with GLFWwindow?

Flighty answered 4/3, 2016 at 10:28 Comment(2)
typedef struct GLFWwindow GLFWwindow; this is for C. In C you need a typedef if you don't want to write struct GLFWwindow every time you need this type.Similar
I think the pointer of the window is a good practice of using unique pointer. The pointer is exclusive own the resource.Flighty
S
16

You'll need to supply the Deleter to the unique ptr:

struct DestroyglfwWin{

    void operator()(GLFWwindow* ptr){
         glfwDestroyWindow(ptr);
    }

};

std::unique_ptr<GLFWwindow, DestroyglfwWin> my_window;

It can be handy to use a typedefed smart pointer.

typedef std::unique_ptr<GLFWwindow, DestroyglfwWin> smart_GLFWwindow;

smart_GLFWwindow my_window;
Strophe answered 4/3, 2016 at 10:45 Comment(2)
Thanks for your answer ratchet, the error message ' C2027 use of undefined type 'GLFWwindow' ' confused me, making me believe I have to have the definition of GLFWwindow to declare an unique pointer. I should really pay attention of the code in memory : static_assert(0 < sizeof (_Ty), "can't delete an incomplete type"); which implies I need a deleter. :D Thank you very muchFlighty
unable to get this to work !Trammel
M
8

but I have no idea why it is not like …

That's because GLFW is written in C and not C++. Those two are completely different languages, with some overlap in which declarations work, but even with that there are major differences, as you've seen.

In C structs and enums each live in their own tag namespace and you have to explicitly write struct name_of_the_struct … or enum name_of_the_enum … to use it. By declaring this type definition typedef struct … … you pull the struct into the regular variable and type namespace. There are hardcore C programmers (including me) who think, that this should not be done. If you're writing C++ you're writing C++ and if you're writing C, you're writing C and there's no danger of mixing those two up.

What is the pro of the former formation of forward declaration?

Saving a few keystrokes when writing C. That's it…

So the real questions, since the structure GLFWwindow is only a declaration, unique pointer can not complete template specialization without definition. I can not use unique_ptr to declare any pointer.

There's more to unique_ptr than just having the struct definition. A unique_ptr (emphasis by me):

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.

Destruction happens by calling delete on the object, which in turn invokes the destructor. In C++ structs are actually all-public-by-default classes so constructor/destructor semantics works as expected. But in plain C a struct is just plain memory. So the semantics of a unique_ptr are not really applicable. You need some helper class, that augments the constructor/destructor semantics. See ratchet_freak's answer for that.

Minda answered 4/3, 2016 at 10:45 Comment(1)
Thanks for your answer, datenwolf. It really helps :)Flighty

© 2022 - 2024 — McMap. All rights reserved.