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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:10:03+00:00 2026-05-26T04:10:03+00:00

Okay, so I’m just learning about templates. Anyways, I’m probably (most definitely) doing something

  • 0

Okay, so I’m just learning about templates. Anyways, I’m probably (most definitely) doing something wrong, but here’s the problem:

My first template function is declared like this:

template<typename T>
std::ostream& printFormatted(T const& container, std::ostream& os = std::cout) {
    //...
}

Then I’m supposed to implement a specialized case for maps, so this is what I tried to do:

template<>
std::ostream& printFormatted<std::map<typename key, typename value>>(std::map<typename key, typename value> const& container, std::ostream& os = std::cout) {
    //...
}

I might be making a mistake with my key/value variables, not sure, but regardless, when trying to compile I get the error message:

error: wrong number of template arguments (1, should be 4)
error: provided for ‘template<class _Key, class _Tp, class _Compare, class _Allocator> class std::__debug::map’

Clearly there’s something I don’t understand about templates or maps? Someone please 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-05-26T04:10:04+00:00Added an answer on May 26, 2026 at 4:10 am

    Assuming your uses of key and value are meant to be placeholers, you cannot declare template parameters inline with the keyword typename. That is to say, Foo<typename T> is always invalid — but not to be mistaken with Foo<typename T::bar> which is different altogether. The syntax for specialization looks like:

    // Declare template parameters up front
    template<typename Key, typename Value>
    std::ostream&
    printFormatted<std::map<Key, Value> >(std::map<Key, Value> const& container, std::ostream& os = std::cout);
    

    but that wouldn’t work because it’s a partial specialization and those are not allowed for function templates. Use overloading instead:

    template<typename Key, typename Value>
    std::ostream&
    printFormatted(std::map<Key, Value> const& container, std::ostream& os = std::cout);
    

    This overload will be preferred over the more general template.

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

Sidebar

Related Questions

Okay, I know I have to be doing something wrong here because the performance
Okay, I know I'm doing something wrong - but can't figure out a better
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
Okay, so this is a long long shot but here it goes... When IE
Okay, so the answer to my question might not be the problem but here's
Okay, so I've known about this for like 600 years now, but I've only
Okay, what have I done wrong here? I'm trying to .POST data from a
Okay so im working on this php image upload system but for some reason
Okay, here's the scenario. I have a utility that processes tons of records, and
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent

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.