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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:19:50+00:00 2026-06-12T13:19:50+00:00

I find something annoying in C++ and I don’t know if there is a

  • 0

I find something annoying in C++ and I don’t know if there is a trick to avoid this with no overhead. The problem is the following :

For a template function, we can have :

// Function declaration/definition
template<bool Option = false> void myFunction() 
{
    std::cout<<"Option = "<<Option<<std::endl;
}

// Then I can use :
myFunction<false>();
myFunction<true>();
myFunction(); // <- NO PROBLEM HERE

Now for a template class :

// Class definition/declaration
template<bool Option = false> class MyClass
{
};

// Then I can use :
myClass<false> x;
myClass<true> y;
myClass z; // <- PROBLEM HERE : only "MyClass<> z;" will compile !

Why is the reason of this behaviour ?
Is there any trick to avoid that ?
For a class with optionnal parameters passed as template, I find this not convenient for the end user : he should be able to use the default implementation as a no-templated class…

  • 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-12T13:19:52+00:00Added an answer on June 12, 2026 at 1:19 pm

    Why is the reason of this behaviour ?

    It’s because functions can be overloaded, and types can’t.

    When you write a function call, the compiler populates an overload set of all the functions it can find with that name, and then figures out which ones match the argument(s) passed. Now, for this to work cleanly with function templates, it allows the template argument types to be deduced from the parameters. Because type parameter inference is allowed in general, it works for your case even when the parameter is defaulted instead.

    Types, however, aren’t overloaded. While myFunction<true>() and myFunction<false>() are both related to the extent they’ll participate in the same overload set, myClass<true> and myClass<false> are separate and unrelated types. With no equivalent of overloading on type names, there’s no motivation to add a special case for implicitly naming a fully-specialized template class. The parameters can never be inferred, so it would amount to special syntax only for the case where they’re all defaulted.

    Is there any trick to avoid that ?

    In general, if you want to get template argument deduction for template classes, you can provide a template function wrapper (this works best with C++11 auto)

    template <bool Option=false> class MyClass {};
    template <bool Option=false> MyClass<Option> make_my_class() {
        return MyClass<Option>();
    }
    // ...
    auto z = make_my_class();
    

    Otherwise, I think using typedef (as per Remy’s comment) is the best option.

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

Sidebar

Related Questions

I am expecting when I go $.each($(something).find(something), function(){ $(this).delay(1000).fadeOut(); }); then for each matching
I find this annoying but is there a better way to retrieve a struct
I didn't find information on this annoying problem, and it seem quite common to
weirdly I cannot find something simple about this, I've got 2 entity, one inside
This is a very simple question, but I can't seem to find something about
My problem is quite annoying, probably something really stupid but I've been going around
I find it annoying that I can't clear a list. In this example: a
I find stuff like this rather annoying and ugly in equals methods: if (field
Admittedly, this is something of a first world problem, but I'm sort of picky
I'm trying to find something similar in MATLAB to PyTables' table.where that selects a

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.