Analysis of the correlation between constant expressions and ellipsis in C + + _c language

Source: Internet
Author: User
Tags class definition variadic

C + + constant expressions
A constant value is a value that does not change. C + + provides two keywords that enable you to express the intention not to modify the object, but also to enable you to implement the intent.
C + + requires a constant expression (an expression that evaluates to a constant) to declare:

    • Array bounds
    • Selectors in case statements
    • Bit-field length specification
    • Enumerating initializers

The only valid operands in a constant expression are:

    • Text
    • Enumeration constants
    • The value of a constant declared to be initialized with a constant expression
    • sizeof expression

You must convert a non-integral constant (explicit or implicit) to a valid integer in a constant expression. Therefore, the following code is legal:

Const double Size = 11.0;
char charray[(int) Size];

An explicit conversion to an integral type is valid in a constant expression; all other types and derived types are illegal (except when used as operands of the sizeof operator).
The comma and assignment operators cannot be used with constant expressions.

Ellipses and variable parameter templates
ellipses have a number of uses in C and C + +. These include a list of variable arguments for the function. The C Run-time Library's printf () function is one of the most common examples.
A variadic template is a class or function template that supports any number of parameters. This mechanism is especially useful for C + + library developers because you can apply it to class templates and function templates to provide a range of types of security and important functionality, as well as flexibility.
The variable parameter template uses ellipses in two ways. The left side of the parameter name represents the parameter package, and the right side of the parameter name expands the parameter package to a separate name.
The following is a basic example of a variable parameter template class definition syntax:

Template<typename ... Arguments> class classname;

As shown in the following example, for parameter boxing and expansion, you can add white space around ellipses, for example:

Template<typename ... Arguments> class classname;

Or as:

Template<typename ... Arguments> class classname;

Note that this article uses the convention shown in the first example (the ellipsis is attached to TypeName).
In the previous example, Arguments is a parameter package. Class ClassName can accept a variable number of arguments, such as the following example:

Template<typename ... Arguments> class Vtclass;

vtclass< > Vtinstance1;
Vtclass<int> Vtinstance2;
Vtclass<float, bool> vtinstance3;
Vtclass<long, Std::vector<int>, std::string> Vtinstance4;

You can also require at least one parameter by using the variable parameter template class definition.

Template <typename, TypeName ... Rest> class classname; 

The following is a basic example of the variable parameter template function syntax:

Template <typename ... Arguments> returntype functionname (Arguments ... args);

As shown in the next section, "Understanding the variable parameter template," the Arguments parameter package is expanded to use.
Variadic template function syntax may also have other forms, including not limited to:

Template <typename ... Arguments> returntype functionname (arguments&. args); 
Template <typename ... Arguments> returntype functionname (arguments&&. args);
Template <typename ... Arguments> returntype functionname (arguments* ... args);

Also allows you to use a descriptor similar to const:

Template <typename ... arguments> returntype functionname (const arguments& args); 

You can create a function that requires at least one parameter, as defined by the variable parameter template class:

Copy Code code as follows:

Template <typename, TypeName ... rest> returntype functionname (const first&, const Rest& ... args);

Variable template using sizeof ... () operator (not related to earlier sizeof () operators):

Template<typename ... arguments>
void Tfunc (const arguments& ... args)
  const unsigned Numargs = sizeof ... (Arguments);

  X Xobj[numargs]; Array of some previously defined type X

  helper_func (xobj, args ...);

More about ellipsis locations
In the past, this article describes the ellipsis position that defines parameter boxing and expansion "to the left of the parameter name, which represents the parameter, the package, and the expansion parameter to the right of the parameter name, which is boxed to a separate name." This is technically true, but may be a fee in converting code. Please consider:
Template parameter list (template <parameter-list>), TypeName ... Describes the template parameter package.
In a parameter declaration statement (func (parameter-list)), the "Top" ellipsis describes the function parameter package, and the ellipsis status is important

V1 is not a function parameter pack:
template <typename ... types> void Func1 (std::vector<types...> v1); 

V2 is a function parameter pack:
template <typename ... types> void Func2 (STD::VECTOR<TYPES>. v2); 

If the ellipsis appears after the parameter name, it has a parameter pack expansion.
A good way to clarify the functional framework of the Variadic template is to use it in the re-write of some features in printf:

#include <iostream>

using namespace std;

void print () {
  cout << Endl;

Template <typename t> void print (const t& t) {
  cout << t << endl;

Template <typename, TypeName ... rest> void print (const first&, const Rest&... Rest) {
  cout << i << ",";
  Print (rest ...); Recursive call using Pack expansion syntax

int main ()
  print ();//Calls-A-overload, Outputt ing only a newline
  print (1);//calls Second overload

  //This call the third overload, the Variadic template,
   //which uses recursion as needed.
  Print (a);
  Print (M, m);
  Print ("A", 2, "third", 3.14159);


2, third, 3.14159.

Most implementations of the merge variable parameter template function use some form of recursion, but it is slightly different from traditional recursion. Traditional recursion involves invoking a function with the same signature as the function. (You can overload or templating, but you must select the same signature each time.) Variable recursion invokes a variable function template with a different (almost always reduced) number of arguments, so each time a different signature is erased. The "base use case" is still required, but the recursive nature is different.

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: 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.