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

  • Home
  • SEARCH
  • 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 745263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:04:27+00:00 2026-05-14T09:04:27+00:00

C# has generic function types such as Action<T> or Func<T,U,V,…> With the advent of

  • 0

C# has generic function types such as Action<T> or Func<T,U,V,...>

With the advent of C++0x and the ability to have template typedef’s and variadic template parameters, it seems this should be possible.

The obvious solution to me would be this:

template <typename T>
using Action<T> = void (*)(T);

however, this does not accommodate for functors or C++0x lambdas, and beyond that, does not compile with the error “expected unqualified-id before 'using'“

My next attempt was to perhaps use boost::function:

template <typename T>
using Action<T> = boost::function<void (T)>;

This doesn’t compile either, for the same reason.

My only other idea would be STL style template arguments:

template <typename T, typename Action>
void foo(T value, Action f) {
    f(value);
}

But this doesn’t provide a strongly typed solution, and is only relevant inside the templated function.

Now, I will be the first to admit that I am not the C++ wiz I prefer to think I am, so it’s very possible there is an obvious solution I’m not seeing.

Is it possible to have C# style generic function types in C++?

  • 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-14T09:04:28+00:00Added an answer on May 14, 2026 at 9:04 am

    I think the syntax should be like this:

    template <typename T>
    using Action = void (*)(T);
    

    I couldn’t get that to compile either, though (g++ v4.3.2).

    The general STL style function template is not strongly typed in the strictest sense, but it is type safe in the sense that the compiler will ensure that the template is only instantiated for types that fulfill all the requirements of the function. Specifically it will be a compiler error if the Action is not callable with one parameter of type T. This a very flexible approach since it doesn’t matter if Action is instantiated to be a function or some class that implements operator().

    The old non-C++0x workaround for the missing templated typedef would be to use a templated struct with a nested typedef:

    template <typename T>
    struct Action {
       typedef boost::function<void (T)> type;
    };
    
    template <typename T>
    void foo(T value, typename Action<T>::type f) {
        f(value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface that has a generic method with two type parameters. I
I have a generic class called Repository. This class has a function that calls
We have a sub-project 'commonUtils' that has many generic code-snippets used across the parent
How can I go about determining return type of a member generic function? template<class
Possible Duplicate: Generic methods and multiple constraints I need a generic function that has
I have the following generic schema to represent different types of information. var Record
Can someone explain this behavior in Generics? I have a generic function in C#
I have this function of mine which selects all room types from the database,
My professor has given me this assignment. Implement a generic function called Max, which
I have a base class with a templated function that has the general templated

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.