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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:25:53+00:00 2026-05-17T21:25:53+00:00

The below code couldn’t pass the compilation, what’s the consideration for this compiler error?

  • 0

The below code couldn’t pass the compilation, what’s the consideration for this compiler error?

template<class T> void f(T t) {};

template<> void f<char>(char c = 'a') {}

Error message: Default arguments are not allowed on an explicit specialization of a function template

  • 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-17T21:25:54+00:00Added an answer on May 17, 2026 at 9:25 pm

    I think that the rationale behind this error is due to the fact that the default arguments in the function template apply to its specialization as well and you are not allowed to define the default argument more than once in C++.

    Consider the following:

    #include <iostream>
    
    template<class T> void f(T t = 'a') {}
    
    template<> void f<char>(char c)
    {
        std::cout << c << std::endl;
    }
    
    int main(int argc, char **argv)
    {
        f<char>();
    }
    

    This will print a meaning that specialization is called with the default argument defined in the main template.

    If you need a different default argument for each specialization you can use the approach illustrated below:

    #include <iostream>
    
    template<class T>
    struct default_arg
    {
        static T get() { return T(); }
    };
    
    template<class T> void f(T t = default_arg<T>::get()) {}
    
    template<>
    struct default_arg<char>
    {
        static char get() { return 'a'; }
    };
    
    template<> void f<char>(char c)
    {
        std::cout << c << std::endl;
    }
    
    int main(int argc, char **argv)
    {
        f<char>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a LinkedList in C, the below code represent my Node definition.
I know there are multiple questions about this same question but I couldn't find
Well guys, here i am. In three days i couldn't resolve this problem. (I'm
I've installed the following module - http://drupal.org/project/og_reg_keys This module adds an additional field to
idLocals is Primary key in database Populating the datatable oDtLocalCharge = bll.GetData(); i am
Today something peculiar happened while debugging in VS 2008. I will give the small
Thank you so much for being so helpful. Owe you all a thank you.
I'm trying to use the AsyncFileUpload control from the Asp.net Ajax Control Toolkit (Sept
I have a form that lets me create new blog posts and I'd like

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.