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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:28:49+00:00 2026-06-14T18:28:49+00:00

Consider a template class like this: template <int opt1 = 1, bool opt2 =

  • 0

Consider a template class like this:

template <int opt1 = 1,
          bool opt2 = false,
          bool opt3 = true,
          int opt4 = 50>
class X  { };

I try to change just one parameter, but it seems that C++ can’t do that. Or am I wrong? How to achieve something like this:

X<opt2 = true> x;
  • 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-14T18:28:50+00:00Added an answer on June 14, 2026 at 6:28 pm

    The Boost version mentioned by R. Martinho Fernandes works something like this:

    1. define a number of option types (they can be empty tag structures, or templated on bool to enable/disable, or whatever)

      template <int I>  struct option_1: public std::integral_constant<int, I>  {};
      template <bool B> struct option_2: public std::integral_constant<bool, B> {};
      template <bool B> struct option_3: public std::integral_constant<bool, B> {};
      template <int I>  struct option_4: public std::integral_constant<int, I>  {};
      
    2. define some kind of typelist of your default values (I’ll hide it in a namespace along with the existing template class)

      namespace impl {
        typedef typelist<option_1<1>, option_2<false>,
                         option_3<true>, option_4<50>> X_defaults;
      
        template <int opt1, bool opt2, bool opt3, int opt4>
        class X { /* ... */ };
      }
      
    3. write a get mechanism to extract an option from a suitable typelist (including a version which falls back on the provided default)

      Once you have all that, you can write a wrapper class that handles all the options and delegates to your existing class, eg.

      template <typename... Options>
      class X: public impl::X<
          get<option_1, typelist<Options...>, impl::X_defaults>::value,
          get<option_2, typelist<Options...>, impl::X_defaults>::value,
          get<option_3, typelist<Options...>, impl::X_defaults>::value,
          get<option_4, typelist<Options...>, impl::X_defaults>::value>
      {
      };
      

    Your final invocation now looks like:

    X<option_2<true>> x;
    

    The typelist and get parts are left as an exercise (along with lots of errors, most likely) for the OP, a kindly passing editor, or me when I have more time.

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

Sidebar

Related Questions

Consider this class template: template <typename T1, typename T2, bool B> class SomeClass {
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
Consider this code: template<typename T> class Base { template<typename U> friend void f(void *ptr)
Consider this simple class: template<class T> class Foo{ public: Foo(T const& val) : _val(val)
Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }
Consider standard algorithms like, say, std::for_each . template<class InputIterator, class Function> Function for_each(InputIterator first,
Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
Consider this simplified example: #include <list> typedef std::list<int> IntList; class KindaIntList { public: IntList::const_iterator

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.