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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:45:11+00:00 2026-06-14T08:45:11+00:00

How should I initialize a static variable for a partial specialization? template <bool A=true,

  • 0

How should I initialize a static variable for a partial specialization?

template <bool A=true, bool B=false>
struct from {
    const static std::string value; 
};

// no specialization - works
template <bool A, bool B>
const std::string from<A, B>::value = "";

// partial specialization - does not compile -  
// Error: template argument list following class template name must list parameters in the order used in template parameter list
// Error: from<A,B>' : too few template arguments
template <bool B>
const std::string from<true, B>::value = "";

// full specialization - works
const std::string from<false, true>::value = "";

Why doesn’t the partial work?

EDIT: I found a solution based on Partial template specialization for initialization of static data members of template classes

I need to repeat the declaration for the partial specialization before it allowed me to initialize the static variable:

template <bool B>
struct from<true, B> {
    const static std::string value; 
};

Again, the question is why?

  • 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-14T08:45:13+00:00Added an answer on June 14, 2026 at 8:45 am

    Partial specialization of members (whether they’re functions or static data) are not allowed without partial specialization of enclosing class template itself.

    That is, you have to specialize the class template also. So the following should work:

    //partial specialization of class template
    template <bool B>
    struct from<true, B> {
        const static std::string value; 
    };
    
    //now you can do this!    
    template <bool B>
    const std::string from<true, B>::value = ""
    

    Also, this will not compile (have you tried compiling this?):

    // full specialization - works (SORRY, IT WILL NOT WORK!)
    const std::string from<false, true>::value = "";  //this should be an error
    

    You’ve to write this:

    // full specialization 
    template<>   //<---- this is important!
    const std::string from<false, true>::value = ""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class with a string instance attribute. Should I initialize this
If I have a static method like this private static bool TicArticleExists(string supplierIdent) {
When should we use an uninitialized static final variable? I know that an uninitialized
I am somewhat confused about memory allocation of static variable in objective C. should
Is there a way to initialize an array like so: static const vec3d<long> XI[Q]
Should I initialize the database cursor once per each Apache thread, or should I
When should one use the f:viewAction or preRenderView event to initialize data for a
I have a confusion in the concepts of static integer.When i initialize a static
Static members confuse me sometimes. I understand how to initialize a simple built in
This is a rather a problem from a convoluted situation. I have a static

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.