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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:30:48+00:00 2026-06-04T07:30:48+00:00

I am trying to construct a std::thread with a member function that takes no

  • 0

I am trying to construct a std::thread with a member function that takes no arguments and returns void. I can’t figure out any syntax that works – the compiler complains no matter what. What is the correct way to implement spawn() so that it returns a std::thread that executes test()?

#include <thread>
class blub {
  void test() {
  }
public:
  std::thread spawn() {
    return { test };
  }
};
  • 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-04T07:30:50+00:00Added an answer on June 4, 2026 at 7:30 am
    #include <thread>
    #include <iostream>
    
    class bar {
    public:
      void foo() {
        std::cout << "hello from member function" << std::endl;
      }
    };
    
    int main()
    {
      std::thread t(&bar::foo, bar());
      t.join();
    }
    

    EDIT:
    Accounting your edit, you have to do it like this:

      std::thread spawn() {
        return std::thread(&blub::test, this);
      }
    

    UPDATE: I want to explain some more points, some of them have also been discussed in the comments.

    The syntax described above is defined in terms of the INVOKE definition (§20.8.2.1):

    Define INVOKE (f, t1, t2, …, tN) as follows:

    • (t1.*f)(t2, …, tN) when f is a pointer to a member function of a class T and t1 is an object of type T or a reference to an object of
      type T or a reference to an object of a type derived from T;
    • ((*t1).*f)(t2, …, tN) when f is a pointer to a member function of a class T and t1 is not one of the types described in the previous
      item;
    • t1.*f when N == 1 and f is a pointer to member data of a class T and t 1 is an object of type T or a
      reference to an object of type T or a reference to an object of a
      type derived from T;
    • (*t1).*f when N == 1 and f is a pointer to member data of a class T and t 1 is not one of the types described in the previous item;
    • f(t1, t2, …, tN) in all other cases.

    Another general fact which I want to point out is that by default the thread constructor will copy all arguments passed to it. The reason for this is that the arguments may need to outlive the calling thread, copying the arguments guarantees that. Instead, if you want to really pass a reference, you can use a std::reference_wrapper created by std::ref.

    std::thread (foo, std::ref(arg1));
    

    By doing this, you are promising that you will take care of guaranteeing that the arguments will still exist when the thread operates on them.


    Note that all the things mentioned above can also be applied to std::async and std::bind.

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

Sidebar

Related Questions

I'm trying to use partial application of function arguments so I can use STL's
I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly
I'm trying to figure out what this piece of code does: std::vector<std::vector<bool> > visited(rows,
I am trying to construct a Groovy statement to find values that don't exist
I am trying to construct a SpannableString such that it looks like this: Two
I have this function that converts an integer to a std::string: std::string intToStr(const int
I am trying to initialise an std::vector<std::unique_ptr<std::string>> in a way that is equivalent to
I'm trying to export classes from a DLL that contain objects such as std::vectors
I am using a std::for_each loop on a local variable of a member function
i am trying to sort vector of struct's elements,but i can't construct vector itself

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.