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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:38:54+00:00 2026-05-19T13:38:54+00:00

Is there a straightforward way for defining a partial specialization of a C++ template

  • 0

Is there a straightforward way for defining a partial specialization of a C++ template class given a numerical constant for one of the template parameters? I’m trying to create special constructors for only certain kinds of template combinations:

template <typename A, size_t B> class Example
{
    public:
        Example() { };

        A value[B];
};

template <typename A, 2> class Example
{
    public:
        Example(b1, b2) { value[0] = b1; value[1] = b2; };
};

This example won’t compile, returning an error Expected identifier before numeric constant in the second definition.

I’ve had a look through a number of examples here and elsewhere, but most seem to revolve around specializing with a type and not with a constant.

Edit:

Looking for a way to write a conditionally used constructor, something functionally like this:

template <typename A, size_t B> class Example
{
    public:
        // Default constructor
        Example() { };

        // Specialized constructor for two values
        Example<A,2>(A b1, A b2) { value[0] = b1; value[1] = b2; };

        A foo() {
          A r;

          for (size_t i = 0; i < b; ++b)
            r += value[i];

          return r;
        }

        // Hypothetical specialized implementation
        A foo<A, 2>() {
          return value[0] + value[1];
        }

        A value[B];
};
  • 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-19T13:38:55+00:00Added an answer on May 19, 2026 at 1:38 pm

    I think this might work:

    #include <iostream>
    
    template <typename A, size_t B>
    class Example {
    public:
        Example()
        {
            Construct<B>(identity<A, B>());
        }
    
        A foo()
        {
            return foo<B>(identity<A, B>());
        }
    
    private:
        template <typename A, size_t B>
        struct identity {};
    
        template <size_t B>
        void Construct(identity<A, B> id)
        {
            for (size_t i = 0; i < B; ++i)
            {
                value[i] = 0;
            }
            std::cout << "default constructor\n";
        }
    
        template <size_t B>
        void Construct(identity<A, 2> id)
        {
            value[0] = 0;
            value[1] = 0;
            std::cout << "special constructor\n";
        }
    
        template <size_t B>
        A foo(identity<A, B> id)
        {
            A r = 0;
            for (size_t i = 0; i < B; ++i)
            {
                r += value[i];
            }
            std::cout << "default foo\n";
            return r;
        }
    
        template <size_t B>
        A foo(identity<A, 2> id)
        {
            std::cout << "special foo\n";
            return value[0] + value[1];
        }
    
        A value[B];
    };
    
    int main()
    {
        Example<int, 2> example; // change the 2 to see the difference
        int n = example.foo();
        std::cin.get();
        return 0;
    }
    

    Sorry, I just copy and pasted it from my test project. It’s not really “specialization” in a way, it just calls overloads to specialized functions. I’m not sure if this is what you want and imo this isn’t very elegant.

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

Sidebar

Related Questions

Is there a straightforward way to query a web service to see which messages
Is there a straightforward way using reflection to get at the parameter list for
Is there any straightforward way to get the mantissa and exponent from a double
Is there a relatively straightforward way to get the intersection of two DataTables in
Is there a straightforward way to list the names of all modules in a
Is there a straightforward way to run mysql_query with an insert and get the
Is there a straightforward way to clean up the directory where xcode deploys an
I hope that there is a very straightforward way to allow a user to
Is there a straightforward way to turn the functions of a .RData file into
Is there any straightforward way of telling the whole WPF application to react to

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.