简体   繁体   English

需要帮助理解此C ++模板的语法

[英]Need help understanding the syntax of this C++ template

I am new to C++ templates and encountered these C++ templates related codes but is not able to understand their meaning: 我是C ++模板的新手,遇到了这些与C ++模板相关的代码,但无法理解它们的含义:

class StringBuffer
{
    CharBuffer cb;
..
    template <size_t ArrayLength>
    bool append(const char (&array)[ArrayLength]) {
        return cb.append(array, array + ArrayLength - 1); /* No trailing '\0'. */
    }
};

What does the bool append(const char (&array)[ArrayLength]) mean? bool追加的是什么(const char(&array)[ArrayLength])是什么意思? It seems to me that the function template will be instantiated to something taking a parameter with a specific ArrayLength. 在我看来,函数模板将被实例化为带有特定ArrayLength参数的东西。 But isn't that we cannot specify an array length in the parameter list of a function? 但是不是我们不能在函数的参数列表中指定数组长度吗? Also what does const char (&array) mean? const char(&array)的含义是什么? Shouldn't it be something like const char & (without the parentheses)? 它不应该像const char& (没有括号)吗?

I am reading the book C++ Templates The Complete Guide by David Vandevoorde/Nicolai M.Josuttis , which part of the book covers the above syntax? 我正在阅读David Vandevoorde / Nicolai M.Josuttis撰写的C ++模板完整指南一书,本书的哪一部分涵盖了上述语法?

It means "reference to const char array". 它的意思是“引用const char数组”。 The reason for it is that if you pass like 原因是如果你通过喜欢

template <int S>
void f(T a[s]){}

You will lose the size information according to "array parameter deprecation rules", mainly because pointer doesn't hold array size information. 您将根据“数组参数弃用规则”丢失大小信息,主要是因为指针不包含数组大小信息。 (AKA standard said so.) So you will have to pass by reference and not by pointer value. (AKA标准如此说。)所以你必须通过引用而不是指针值传递。

The parenthesis before [] is required because [] will take precedence in front of &, so in order to make & take precedence it needs to be done like []之前的括号是必需的,因为[]将优先于&前面,所以为了使它成为优先级,它需要像

 T (&a)[s]
const char (&array)[ArrayLength]

is a reference to an array of ArrayLength objects of type char . 是对char类型的ArrayLength对象数组的引用。

Without the parentheses, it would be an array of references, which is not allowed. 没有括号,它将是一个引用数组,这是不允许的。 Without the & , it would be an array which (as a function parameter) decays to a pointer, losing the information about the size of the array. 没有& ,它将是一个数组(作为函数参数)衰减到指针,丢失有关数组大小的信息。

It seems to me that the function template will be instantiated to something taking a parameter with a specific ArrayLength. 在我看来,函数模板将被实例化为带有特定ArrayLength参数的东西。

That's right. 那就对了。 The array length is known at compile time, and this will instantiate a function that can use that compile-time value. 数组长度在编译时是已知的,这将实例化一个可以使用该编译时值的函数。

But isn't that we cannot specify an array length in the parameter list of a function? 但是不是我们不能在函数的参数列表中指定数组长度吗?

Yes, you could supply the length as an extra function parameter; 是的,您可以提供长度作为额外的功能参数; but that would be a runtime value, and there'd be know way to validate that it was correct. 但这将是一个运行时值,并且有知道验证它是否正确的方法。 The template ensures that the template argument really is the size of the array. 模板确保模板参数确实是数组的大小。

which part of the book covers the above syntax? 本书的哪一部分涵盖了上述语法?

I don't have that book but, looking at the table of contents I'd suggest looking at 4.2 (Nontype Function Template Parameters) and 11 (Template Argument Deduction) for this kind of thing. 我没有那本书,但是,看一下目录,我建议看一下这种东西的4.2(非类型函数模板参数)和11(模板参数演绎)。

It's the syntax for passing the array by reference (since arrays can't be passed by value in C++): 它是通过引用传递数组的语法(因为数组不能通过C ++中的值传递):

void foo(const char (&array)[10]) { ... } // We can pass an array of lenth 10

Now throw a template parameter in the mix instead of the 10. The compiler knows the size of an array at compile time and can instantiate the template with correct value. 现在在混合中抛出模板参数而不是10.编译器在编译时知道数组的大小,并且可以使用正确的值实例化模板。

template<size_t N>
void foo(const char (&array)[N])
{
    // use N, it'll be whatever the size of the array you instantiate the template with is
}

The given code is a good lesson: at first , It want to pass a array , So can't pass by value, then pass by refrence (&) , Then it pass it by const word that safe pass it. 给定的代码是一个很好的教训:首先,它想要传递一个数组,所以不能通过值传递,然后通过引用(&)传递,然后它通过安全传递它的const字传递它。 You know C/C++ has limitation in array, So programmer of this code defined a template for length of Array, and solve this problem. 你知道C / C ++在数组中有局限性,所以这段代码的程序员为Array的长度定义了一个模板,并解决了这个问题。

This syntax will set a template parameter based on the size of a statically allocated array argument. 此语法将根据静态分配的数组参数的大小设置模板参数。

The templated version of "append" (that you included) calls an overload which takes 2 arguments: a pointer to char and a count (you did not include this). 模板化的“append”(包括你)调用一个带有2个参数的重载:一个指向char和一个count的指针(你没有包含它)。

So you might have an array like: 所以你可能有一个像这样的数组:

const char my_string[] = "hi";

You would use the "append" member function like this: 您可以使用“append”成员函数,如下所示:

my_string_buffer_object.append(my_string);

And the length of my_string will be auto-detected, setting the ArrayLength parameter to the length of my_string. 并且将自动检测my_string的长度,将ArrayLength参数设置为my_string的长度。 Then a more verbose version of "append" is called with the string length automatically filled in for you. 然后调用更详细的“append”版本,并自动填充字符串长度。

Basically, this version of "accept" wraps another version. 基本上,这个版本的“接受”包装了另一个版本。 It lets you pass an array as the only argument, automatically filling in a length using the template parameter's info. 它允许您将数组作为唯一参数传递,使用模板参数的信息自动填充长度。

If you use this syntax, keep in mind that these array length parameters count elements and not object sizes (what sizeof would tell you about the array). 如果使用此语法,请记住这些数组长度参数计算元素而不是对象大小(sizeof会告诉您有关数组的信息)。 For char these are the same, but arrays with larger-sized element types will produce a template array length parameter smaller than its sizeof. 对于char,它们是相同的,但具有较大大小的元素类型的数组将产生小于其sizeof的模板数组长度参数。

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

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