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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:13:55+00:00 2026-06-05T23:13:55+00:00

I am writing a templated class with one type paramenter and one boolean, here

  • 0

I am writing a templated class with one type paramenter and one boolean, here is the code:

template<class T, bool p = true>
class A
{
private:
    T* ptr;
public:
    A();
};


template<class T>
A<T,true>::A()
{
    ptr = 0xbaadf00d;
}

int main()
{
    A<int> obj;
    A<int, false> o;
    return(0);
}

And I am getting these compilation errors:

Error   1   error C3860: template argument list following class template name must list parameters in the order used in template parameter list tst.cpp 15
Error   2   error C2976: 'A<T,p>' : too few template arguments  tst.cpp 15

What am I doing wrong?
Or is it for some reason forbidden to specialize non-type parameters partially?

At the same time if I use the boolean parameter in an if statement, I’m getting this warning:

Warning 1   warning C4127: conditional expression is constant

So I am supposed to do specializations for this kind of things…

Any help would be highly appreciated! 🙂

Thanks in advance!!!!

  • 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-05T23:13:57+00:00Added an answer on June 5, 2026 at 11:13 pm

    You are trying to partially specialize a method. That is not allowed. You can only partially specialize the whole class. Once you have specialized the class, you can then provide a non-specialized definition of the methods for the partially specialized class.

    Here is an example of some code that might do what you want:

    template<class T, bool p = true>
    class A
    {
    private:
        T* ptr;
    public:
        A();
    };
    
    template <class T>
    class A<T,true> {
    private:
        T* ptr;
    public:
        A();
    };
    
    
    template <class T>
    class A<T,false> {
    private:
        T* ptr;
    public:
        A();
    };
    
    template<class T>
    A<T,true>::A()
    {
        ptr = reinterpret_cast<T *>(0xbaadf00d);
    }
    
    template<class T>
    A<T,false>::A()
    {
        ptr = 0;
    }
    
    int main()
    {
        A<int> obj;
        A<int, false> o;
        return(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a template class and at one point in my code would like
I am writing a template class which takes a floating-point-like type (float, double, decimal,
I am writing a template Polynom<T> class where T is the numeric type of
I'm converting my fields class read functions into one template function. I have field
For instance, I have just found myself writing the following traits like class: template<class
Is it possible to implement a class template in such a way that one
Assuming I had two classes, the first one for writing primitive types ( bool
I'm writing a template in xslt with xpath 1.0 and need to access the
I'm writing an XSLT template that need to output a valid xml file for
Does anyone have a template for writing a decent equals method - I remember

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.