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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:17:18+00:00 2026-06-17T15:17:18+00:00

Say I have the following code: #include <iostream> #include <functional> template <int func(int)> struct

  • 0

Say I have the following code:

#include <iostream>
#include <functional>

template <int func(int)>
struct S : std::unary_function<int, int>
{
    int operator()(int x) const
    {
        return func(x);
    }
};

int foo(int x)
{
    return x;
}

int main()
{
    S<foo> s;

    std::cout << s(42) << std::endl;
}

This works okay as a way of wrapping up a function inside of a functor, which means it can be used in other templated functions (like sort, for example (assuming the functor had the right signature)). I don’t want to create a functor struct for every possible return/argument type (and realistically I can’t), and so I tried the following:

template <template <typename R, // Make the return type and argument type template parameters!
                    typename A> R func(A)>
struct S : std::unary_function<R, A>
{
    R operator()(A arg) const
    {
        return func(arg);
    }
};

That didn’t work; it gave me compilation errors. So then I tried:

template <typename R, typename A, R func(A)>
struct S : std::unary_function<R, A>
{
    R operator()(A arg) const
    {
        return func(arg);
    }
};

Which did work. Unfortunately though, I had to change instantiations of S to be S<int, int, foo> s; instead of the nicer S<foo> s;.

Is it at all possible to templatize the function passed as a template argument such that I can do S<foo> s; and not hard code the return type and argument type of the function in S?

My google-foo hasn’t been able to find a specific answer.

Edit: Now I’m wondering if this isn’t possible. I just thought of “what if foo is an overloaded function?” There wouldn’t be, as far as I know, a way to know which foo to use when saying S<foo> s; and thus explicitly stating return/argument type is necessary. Is this correct thinking, and does this mean that the answer to my first question is “No, it’s not possible”?

  • 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-17T15:17:19+00:00Added an answer on June 17, 2026 at 3:17 pm

    You seem to want to have a non-type template template parameter. However, the only legal syntax for template template parameters is template < template-parameters > class. (“A template-argument for a template template-parameter shall be the name of a class template or an alias template, expressed as id-expression.” § 14.3.3)

    You could create a templated class whose constructor argument was a function pointer, but I’m guessing that you’re worried that will create an indirect function call.

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

Sidebar

Related Questions

I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
Lets say we have the following code: #include <iostream> #include <string> struct A {
I have the following code in C++ here: #include <iostream> int main(int argc, const
Say I have the following code: #include <iostream> using namespace std; class Account {
Say I have the following code: #include <string> using namespace std; string GetPath(const string
Say I have the following code: struct date { int day; int month; int
Consider the following code: #include <iostream> struct A { const char *name; A() :
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Say I have the following c program: #include <stdio.h> int main() { printf(Hello world
Say I have the following program #include <stdio.h> #include <stdlib.h> int main(void) { int

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.