Array parameters of C + +

Source: Internet
Author: User

1. Problem, recently look at the Project log module, _log a parameter in the template function actually look at the X. The prototype of the function is this:

template<size_t size>
void _log (char (&strdest) [size], const char *scetion, const char *key, const char *msg, va_list &parm);

Right! That is, char (&strdest) [size] is not read. This comes in as a string array.

2. Open the C + + Bible <c++primer> and find the answer. The non-type parameters and array reference type parameters in the design template type parameter.

First, the array is used as a formal parameter, both as a reference and as a non-reference. In general, we use a non-reference form, that is, the parameter is defined as a pointer to the array element type, a total of three equivalent forms:

1.void func (int *p);

2.void func (int p[]);

3.void func (int p[10]);

The third one shows that when the function is called, the passed array has a maximum of 10 elements. However, the compiler ignores the parameter lengths specified for any array, in other words, 10 of the third function declaration is actually ignored by the compiler.

When we call the upper function, when passing the array name, the array name is automatically converted to a pointer to the first element of the array. That is, the pointer holds the address of the first element of the array. The parameter then copies the address that the pointer points to.

Then, when the array is used as a formal parameter, in the form of a reference, this is the way:

1.void func (int (&p) [10]);

When you call this function, you must pass an array that has 10 elements of type int . Like this

int a[10] = {};

Func (a);

If such an int b[2] = {}; Func (b); is incorrect, because B is not a 10 element. When a formal parameter is a reference type, it is important to understand the meaning of the reference. A reference is the alias of a variable, but it is the variable itself.

At the same time, when using the reference, there is no duplication of the link, most of the time will save space, improve efficiency.

3. However, we can see that the reference type parameter of the array of the above form is very inconvenient. Because of the double restriction of type and length when passing arrays.

So is there a way to solve these problems? Can you use a bit of reference, while avoiding the limitations of length?

The answer is to use 1. Template functions in and use non-type template parameters.

template<size_t size>

void func (int (&p) [size]) {...}

The Bible says that the template non-type parameter is a constant value inside the template definition, and when a constant expression is required, you can specify the length of the array using a non-type parameter, and when the Func is called, the compiler computes the value of the non-type parameter from the argument of the array, which is what the compiler does for us

The value of size is calculated so that we do not pass the length ourselves. This way, you can pass the same array as the array elements, without worrying about the length. Thus avoiding the disadvantages of 2.

So:

int a[10], b[2];

Func (a), func (b) can be called correctly, and the value of size is the array length of a and B.

Another point to note, although using the template's non-type parameters, allows us to omit the limit of the array length, but in fact, the compiler will produce two Func instances func<10> func<2>.

If at this time declare int c[10]; Func (c), at which point A and C will use the same instance.

This is based on: for non-type parameters of the template, the expression that evaluates to the same shape will be considered equivalent.

So a and C call func (int (&p) [ten]), and B calls func (int (&p) [2]).

4. At this point, the first parameter in the _log function has been clarified-the application type parameter of the array. The restriction of the length of the array is removed by using the template's non-type parameters.

The level is limited, welcome to correct mistakes.

 

  

  

Array parameters of C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.