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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:46:00+00:00 2026-05-27T19:46:00+00:00

I have a template member function with this signature: template<typename T> void sync(void (*work)(T*),

  • 0

I have a template member function with this signature:

template<typename T> void sync(void (*work)(T*), T context);

It can be called with a pointer to a function that accepts an argument of type T*. context is passed to that function. The implementation is this:

template<typename T> void queue::sync(void (*work)(T*), T context) {
  dispatch_sync_f(_c_queue, static_cast<void*>(&context),
                  reinterpret_cast<dispatch_function_t>(work));
}

It uses reinterpret_cast<> and it works. The problem is that the standard doesn’t define it very well and it is very dangerous. How can I get rid of this? I tried static_cast but that gave me a compiler error:

static_cast from void (*)(std::__1::basic_string<char> *) to dispatch_function_t (aka void (*)(void *)) is not allowed.

dispatch_function_t is a C type and is the same as void (*)(void*).


I’m not sure I was clear enough. What dispatch_sync_f does is it calls a given callback function and passes the given context parameter to that callback function. (It does that on another thread, although that is out of the scope of this question.)

  • 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-05-27T19:46:00+00:00Added an answer on May 27, 2026 at 7:46 pm

    The reason this is not supported by static_cast is because it is
    potentially unsafe. While a std::string* will convert implicitely to
    a void*, the two are not the same thing. The correct solution is to
    provide a simple wrapper class to your function, which takes a void*,
    and static_casts it back to the desired type, and pass the address of
    this wrapper function to your function. (In practice, on modern
    machines, you’ll get away with the reinterpret_cast, since all
    pointers to data have the same size and format. Whether you want to cut
    corners like this is up to you—but there are cases where it’s
    justified. I’m just not convinced that this is one of them, given the
    simple work-around.)

    EDIT: One additional point: you say that dispatch_function_t is a C type. If this is the case, the actual type if probably extern "C" void (*)(void*), and you can only initialize it with functions that have "C" linkage. (Again, you’re likely to get away with it, but I’ve used compilers where the calling conventions were different for "C" and "C++".)

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

Sidebar

Related Questions

I have a templated class like this: template <typename T> class AguiEvent { std::vector<std::tr1::function<void(T,
I have a template class that has a template member function that needs to
I wonder if it is a good practice to have a member template function
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,
I have a problem with class member function templates. It seems that gcc is
I have a member function of a template class declared as such: template <class
I have template < typename threadFuncParamT > class ThreadWrapper { public: static int ThreadRoutineFunction(void*
I have a class template Foo<T> . I'd like to implement a non-member function
I have this function template template < class TParam > bool greaterThan (TParam A,

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.