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 8967311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:12:52+00:00 2026-06-15T17:12:52+00:00

I want to use boost::call_once() to achieve a thread-safe lazy-construction singleton scenario, however, the

  • 0

I want to use boost::call_once() to achieve a thread-safe lazy-construction
singleton scenario, however, the base singleton class has many derived classes thus the getInstance() function takes an argument to determine which derived class to initialize. The code looks like,

Singleton * Singleton::getInstance(Input * a) {
if (!instance) {
    instance = buildme(a);  //buildme() will return a derived class type based on input a.
    }
return instance;
}

I want to use boost::call_once(), but looks like it can only be used on functions with no arguments void (*func)(). If anybody knows about an alternative solution here please help.

Thanks.

EDIT::

Another question, how to call a non-static member function using call_once? I have a non-static init() member function of this class, but I couldn’t find a correct syntax for calling it using boost::call_once(). Or should I make init() and everything used in it static?

Thanks.

  • 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-15T17:12:53+00:00Added an answer on June 15, 2026 at 5:12 pm

    You can bind additional function parameters to a functor object using boost::bind. Like this:

    Input* input = ???;
    boost::call_once(flag, boost::bind(&Singleton::getInstance, input));
    

    You can use boost::bind to call non-static member functions as well, by passing the instance of the class on which you want to call the function to boost::bind.

    class Foo
    {
    public:
       void func(int) { /* do something */}
    };
    
    Foo f;
    boost::call_once(flag, boost::bind(&foo::func, &f, 10));
    

    With C++11, you can use std::bind, here’s another example. boost::bind is quite similar.

    #include <utility>
    #include <functional>
    #include <iostream>
    #include <string>
    
    void f(int x)
    {
       std::cout << "f(" << x << ")\n";
    }
    
    void g(int x, const std::string& y)
    {
       std::cout << "g(" << x << ", " << y << ")\n";
    }
    
    
    int main()
    {
       auto ten = std::bind(&f, 10);
       auto example = std::bind(&g, 20, "Twenty");
    
       ten();
       example();
    
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use boost::crc so that it works exactly like PHP's crc32() function.
I want to use boost::timed_wait to wait for an event or to timeout after
I am using boost build in my project and now i want to use
I want use javascript setInterval function to achieve a box rotate animate effect, I
I use boost factory to create products, what I want to do is to
I want to use boost::bind to create a boost::function inserting a new key-value pair
I want to use a boost::function and pass it to a function to function
I want to use Boost core libraries (like lambda) and Boost ASIO library in
I want to call the base class implementation of a virtual function using a
Is it possible to use boost::fusion::invoke function to call a function that has default

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.