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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:17:04+00:00 2026-06-09T21:17:04+00:00

I first have this simple example that works (my first time using maps). The

  • 0

I first have this simple example that works (my first time using maps). The type of the name-value pairs is a string to a function pointer respectively.

#include <iostream>
#include <map>
using std::cout;

int foo() {
    return 243;
}

int main() {

    std::map<std::string, int (*)()> list;

    list["a"] = foo;

    cout << list["a"](); // 243

}

Then I tried using a template to specify the type. Where it says int in the map instantiation is where I’d like to specify the type using a template. So I tried but I don’t exactly know where to put <int> where I’m either calling the function or making the name-value pairs. This is what I tried:

#include <iostream>
#include <map>
using std::cout;

int foo() {
    return 243;
}

int main() {

    template <typename N>
    std::map<std::string, N (*)()> list;

    list["A"] = <int> foo; // right here where you see <int>

    cout << list["A"]();

}

This doesn’t work because I don’t think I’m putting <int> in the right place. The errors I’m getting are:

/tmp/134535385811595.cpp: In function 'int main()':
/tmp/134535385811595.cpp:11: error: expected primary-expression before 'template'
/tmp/134535385811595.cpp:11: error: expected `;' before 'template'
/tmp/134535385811595.cpp:14: error: 'list' was not declared in this scope
/tmp/134535385811595.cpp:14: error: expected primary-expression before '<' token
/tmp/134535385811595.cpp:14: error: 'N' was not declared in this scope

Can anyone help?

  • 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-09T21:17:06+00:00Added an answer on June 9, 2026 at 9:17 pm
    template <typename N>
    std::map<std::string, N (*)()> list;
    

    template<typename> syntax is used for definitions of templates. Map class template is already defined elsewhere, you can only provide template parameters when you’re instantiating it.

    You can do what you want by wrapping your map in a class template:

    template<typename ReturnType>
    struct Wrapper {
        std::map<std::string, ReturnType (*)()> m;
    };
    

    and then instantiate and use it like:

    int foo() { }
    
    Wrapper<int> w;
    w.m["foo"] = foo;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function called ProperCase that takes a string, then converts the first
Using Dozer to map two objects, I have: /** /* This first class uses
I have this issue: first, I execute a SQL query using Java and then
Here is the url that I have that works: example.com/php/statePage.php?region_slug=new-york-colleges Here is what I
I am new to Jaxb and this is my first time using it. I
I am using gettimeofday() to time a simple matrix multiply example, but I'm getting
I have this middleware which is the first: class RedirectIt require net/https require uri
I have this class file call SMSHelper.cs First I just wanted to know is
I have this code: tableList = [[NSMutableArray alloc] initWithObjects:@First View,@Second View,nil]; I have synthesized
I have this file which I need to read the first bytes to check

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.