"..." in the function prototype

I saw that someone with C ++ code has a function declaration, as shown below:

void information_log( const char* fmt , ...)

or catch block, for example

catch(...)
{
}

What does "..." mean?

+3
source share
6 answers

Ellipsis ..., in the prototype of a function, is used to designate a function as variational. That is, it allows you to pass a variable number of arguments to a function. In this form, the function should determine some way for the user to specify exactly how many arguments they presented, since the variational library functions in C ++ cannot dynamically determine this information.

For example, the stdio function printfis one such function with a prototype:

int printf(const char *format, ...);

, information_log, , printf , , printf .

:

// cstdarg provides access to the arguments passed to the ellipsis
#include <cstdarg> // or (#include <stdarg.h>)
#include <cstdio>
#include <cstring>

// Concatenates as many strings as are present
void concatenate(char ** out, int num_str, ...)
{
    // Store where the arguments are in memory
    va_list args;

    // Find the first variadic argument, relative to the last named argument
    va_start(args, num_str);

    int out_len = 0;
    int * lengths = new int[num_str];
    char ** strings = new char*[num_str];

    // Extract the strings from the variadic argument list
    for(int i = 0; i < num_str; i++)
    {
        // Specify the position in the argument list and the type
        // Note: You must know the type, stdarg can't detect it for you
        strings[i] = va_arg(args, char *);
        lengths[i] = strlen(strings[i]);
        out_len += lengths[i];
    }

    // Concatenate the strings
    int dest_cursor = 0;
    (*out) = new char[out_len + 1];
    for(int i = 0; i < num_str; i++)
    {
        strncpy( (*out) + dest_cursor, strings[i], lengths[i]);
        dest_cursor += lengths[i];
    }
    (*out)[dest_cursor] = '\0';

    // Clean up
    delete [] strings;
    delete [] lengths;
    va_end(args);
}

int main()
{
    char * output = NULL;

    // Call our function and print the result
    concatenate(&output, 5, "The ", "quick", " brown ", "fox ", "jumps!\n");
    printf("%s", output);

    delete [] output;
    return 0;
}
+8

, .: -)

. , , , C printf, . .

catch (...) , . ( catch, "catch-all".)

+7

" -". , catch.

, . API- stdarg.h .

+3

$5.2.2/6 - " ( (8.3.6)) ( ,... 8.3.5), (8.4). [: , , (...), .]"

"information_log" OP

$15.3/6 - "A... ... ; . ,... try."

, catch catch catch all handler.

void f(){
    try{
        throw 2.2;       // throw double
    }
    catch(int){}              // standard conversion from double to int not permitted
    catch(...){
        cout << "catch it here";   // is caught here in catch all clause
    }
}

int main(){
    f();
}
+3

.

Using ellipsis, ..., with C ++ function prototypes means that a function can be specified using an unknown number and type of parameters . This function can be used to suppress parameter type checking and resolution flexibility in the interface function. C ++ allows functions declared with an undefined number of arguments.

+1
source

Source: https://habr.com/ru/post/1762383/


All Articles