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

  • Home
  • SEARCH
  • 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 6647231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:30:46+00:00 2026-05-26T00:30:46+00:00

I am studying C++ concepts – Template specialization and Partial template specialization. Have a

  • 0

I am studying C++ concepts – Template specialization and Partial template specialization. Have a code as below, which I want to understand so I get these concepts correctly.

I have few questions regarding that, which are asked inline below:

template <typename T, int nSize>
class Buffer
{
private:   
    T m_atBuffer[nSize];

public:
    T* GetBuffer()
    {
        return m_atBuffer;
    }

    T& operator[](int nIndex)
    {
        return m_atBuffer[nIndex];
    }
};


template <typename T, int nSize>
void PrintBufferString(Buffer<T, nSize> &rcBuf)
{
    std::cout << rcBuf.GetBuffer() << std::endl;
}


void PrintBufferString(Buffer<char, 10> &rcBuf)
{
    std::cout << rcBuf.GetBuffer() << std::endl;
}


int main()
{
    // declare a char buffer
    Buffer<char, 13> cChar10Buffer;

    // copy a value into the buffer
    strcpy(cChar10Buffer.GetBuffer(), "Ten");

    PrintBufferString(cChar10Buffer); //This prints "Ten"

    // declare an int buffer
    Buffer<int, 10> cInt10Buffer;

    // copy values into the buffer
    for (int nCount=0; nCount < 10; nCount++)
        cInt10Buffer[nCount] = nCount;

    PrintBufferString(cInt10Buffer); // This prints address of the buffer- m_atBuffer for object cInt10Buffer

    return 0;
}

So if we pass any type other than char to the templated function PrintBufferString() it cout prints the address of the buffer rather than the string, which is a problem.

So to solve this problem, it said that we define a template specialization as shown below, to ensure that only arrays of type char can be passed to PrintBufferString()

void PrintBufferString(Buffer<char, 10> &rcBuf)
{
    std::cout << rcBuf.GetBuffer() << std::endl;

}

Question 1: So adding this template specialization for function PrintBufferString (), I thought that it should have given a compilation error when I tried to call

PrintBufferString(cInt10Buffer) by passing a Buffer object with templated type parameter int, but it compiled fine? How is that? Then what is the use of adding this template specialization for type char?

I thought adding a template specialization for type char, we cannot call it for any other type

Question 2: Then I added another function call as below in main:

Buffer<char, 11> cChar11Buffer;
strcpy(cChar11Buffer.GetBuffer(), "Eleven");

PrintBufferString(cChar11Buffer); //

It said this would give compilation error, but it compiled fine in MS -Visual C++ 2010 Express.IT even executed fine and printed “Ten” “some address” “Eleven”.

Why did it compile and execute fine? Because I had understanding that type a Class Buffer is different than Class Buffer, and function
PrintBufferString() accepts object of class type Buffer, and both of these cannot be intermixed?

Question 3: Then it went on to define a partial template specialization as below to handle the case when a object buffer of type char but any size can be passed Class object type, which is then passed to the function PrintBufferString();

template<int nSize>
void PrintBufferString(Buffer<char, nSize> &rcBuf)
{
    std::cout << rcBuf.GetBuffer() << std::endl;
}

Now also it printed “Ten” “Eleven” just as before. So what special did this partial template specialization achieve? Is this not a good example of partial template specialization? The concept is not very clear to me, from this example.

  • 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-26T00:30:47+00:00Added an answer on May 26, 2026 at 12:30 am

    C++ doesn’t have/support partial specialization of function templates, only of class templates. As such, none of the code you’re looking at is related to what you (at least say you) want to study/understand at all.

    A partially specialized class template would be something like this:

    template <class T, class U>  // the "base" (unspecialized) template
    class X { 
    };
    
    template <class T>            // partial specialization for `X<whatever, int>`
    class X<int> {
    };
    

    Note that (as here) the un-specialized template must precede the partially specialized template.

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

Sidebar

Related Questions

While studying C# in ASP.net I have trouble understanding several classes. In which scenario
these days I have been studying about NP problems, computational complexity and theory. I
While studying about JMX, I have seen one of the important feature of it
After studying TCP/UDP difference all week, I just can't decide which to use. I
I have been studying the ECMAScript specification and have found that it is extremely
When studying a snippet of unknown Python code, I occasionally bump into the varName.methodName()
After spending a couple months studying DDD methodology, I've now began to apply these
I am currently studying Fiege-Fiat Shamir and am stuck on quadratic residues. I understand
Studying STL I have tried to negate a functor with not2 but encontered problems.
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,

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.