Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:59:39+00:00 2026-06-17T14:59:39+00:00

I’ve written a RAII wrapper for C function pairs which initialize and release resources

  • 0

I’ve written a RAII wrapper for C function pairs which initialize and release resources and it serves me well for most cases.

#include <GL/glfw.h>
#include <string>
#include <functional>
#include <stdexcept>

template <typename UninitFuncType,
          typename SuccessValueType,
          SuccessValueType successValue>
class RAIIWrapper
{
public:
    template <typename InitFuncType, typename... Args>
    RAIIWrapper(InitFuncType initializer,
                UninitFuncType uninitializer,
                const std::string &errorString,
                const Args&... args) : 
        uninit_func(uninitializer)
    {
        if (successValue != initializer(args...))
            throw std::runtime_error(errorString);
        initialized = true;
    }

    bool isInitialized() const
    {
        return initalized;
    }

    ~RAIIWrapper()
    {
        if (initalized)
            uninit_func();
    }

    // non-copyable
    RAIIWrapper(const RAIIWrapper &) = delete;
    RAIIWrapper& operator=(const RAIIWrapper &) = delete;

private:
    bool initalized = false;
    std::function<UninitFuncType> uninit_func;
};

using GLFWSystem = RAIIWrapper<decltype(glfwTerminate), decltype(GL_TRUE), GL_TRUE>;
using GLFWWindow = RAIIWrapper<decltype(glfwCloseWindow), decltype(GL_TRUE), GL_TRUE>;

int main()
{
    GLFWSystem glfw(glfwInit,
                    glfwTerminate,
                    "Failed to initialize GLFW");
}

However, say when a function returns void like Enter/LeaveCriticalSection I’m not sure how to go about and do it in this class. Should I specialize the class for SuccessValueType = void case? Or something with default template parameter should do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:59:40+00:00Added an answer on June 17, 2026 at 2:59 pm

    I’d like to note, that

    1. You do not need information on your initialization function in your wrapper class. You only need to know about uninitialization function.

    2. You can create function helpers to instantiate your wrapper.

    I came up with the following solution (I liked @ipc exception handling idea)

    template <typename UninitF>
    struct RAII_wrapper_type
    {
        RAII_wrapper_type(UninitF f)
        :_f(f), _empty(false)
        {}
        RAII_wrapper_type(RAII_wrapper_type&& r)
        :_f(r._f), _empty(false)
        {
          r._empty = true;
        }
    
        RAII_wrapper_type(const RAII_wrapper_type&) = delete;
        void operator=(const RAII_wrapper_type&) = delete;
    
        ~RAII_wrapper_type()
        {
          if (!_empty) {
            _f();
          }
        }
    
      private:
        UninitF _f;
        bool _empty; // _empty gets true when _f is `moved out` from the object.
    };
    
    template <typename InitF, typename UninitF, typename RType, typename... Args>
    RAII_wrapper_type<UninitF> capture(InitF init_f, UninitF uninit_f, RType succ, 
                                       const char* error, Args... args)
    {
      if(init_f(args...) != succ) {
        throw std::runtime_error(error);
      }
      return RAII_wrapper_type<UninitF>(uninit_f);
    }
    
    template<typename InitF, typename UninitF, typename... Args>
    RAII_wrapper_type<UninitF> capture(InitF init_f, UninitF uninit_f, Args... args)
    {
      init_f(args...);
      return RAII_wrapper_type<UninitF>(uninit_f);
    }
    

    Example:

    void t_void_init(int){}
    int t_int_init(){ return 1; }
    void t_uninit(){}
    
    int main()
    {
      auto t1 = capture(t_void_init, t_uninit, 7);
      auto t2 = capture(t_int_init, t_uninit, 0, "some error");
    }
    

    Edit

    RAII_wrapper_type should have move semantics and we should carefully implement its move constructor to prevent uninit_f from calling several times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.