简体   繁体   English

有关C ++模板专业化和部分模板专业化的查询

[英]Query about C++ template specialization and partial template specialization

I am studying C++ concepts - Template specialization and Partial template specialization. 我正在研究C ++概念-模板专业化和部分模板专业化。 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. 因此,如果我们将除char以外的任何其他类型传递给模板化函数PrintBufferString(),它会打印出缓冲区的地址而不是字符串,这是一个问题。

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() 因此,为了解决这个问题,它说我们定义了一个模板专门化,如下所示,以确保只能将char类型的数组传递给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 问题1:因此,为函数PrintBufferString()添加了此模板专业化功能,我认为当我尝试调用它时应该给出编译错误

PrintBufferString(cInt10Buffer) by passing a Buffer object with templated type parameter int, but it compiled fine? 通过传递带有模板化类型参数int的Buffer对象,从而实现PrintBufferString(cInt10Buffer),但编译良好吗? How is that? 那个怎么样? Then what is the use of adding this template specialization for type char? 那么为char类型添加此模板专用化有什么用?

I thought adding a template specialization for type char, we cannot call it for any other type 我以为为char类型添加模板专业化,我们不能为其他任何类型调用它

Question 2: Then I added another function call as below in main: 问题2:然后我在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". 它说这会产生编译错误,但在MS -Visual C ++ 2010 Express.IT中编译良好,甚至可以正常执行并打印“十”,“某些地址”,“十一”。

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? 因为我知道类缓冲区的类型不同于类缓冲区,并且函数PrintBufferString()接受类类型为Buffer的对象,而这两个对象不能混在一起?

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(); 问题3:然后继续定义如下的部分模板特殊化,以处理以下情况:可以传递char类型但大小不限的对象缓冲区,然后将Class对象类型传递给函数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. 从这个例子中,这个概念对我来说不是很清楚。

C++ doesn't have/support partial specialization of function templates, only of class templates. C ++没有/支持功能模板的部分专业化,仅支持类模板。 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. 请注意(如此处所示),非专用模板必须位于部分专用模板之前。

In your first case, the function is not a specialization, it's just overloading. 在您的第一种情况下,该函数不是专门化的,只是重载。

If you want to prevent other type than char, just define the version in question 3. As long as you don't prefix your function by template<>, it will no be considered a partial specialization. 如果要防止使用char类型以外的其他类型,只需定义问题3的版本即可。只要不使用template <>作为函数的前缀,就不会将其视为部分专业化。

You can define function that uses template types with some fixed templates argument. 您可以定义使用带有某些固定template参数的模板类型的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM