What is the use of the Variadic constructor in C ++?

Consider the following program:

#include <iostream>
struct Test
{
    Test(...)
    {
        std::cout<<"Variadic constructor\n";
    }
};
int main()
{
    Test t;
    t={3,4,5};
}

I think this is a variational constructor. Does the C ++ standard mean that a constructor can be a variable? What is the use of such a constructor? What is the rationale for authorizing a Variadic constructor?

+4
source share
1 answer

Try to answer your questions one by one:

I think this is a variational constructor.

You're right.

Does the C ++ standard know that a constructor can be a variable?

IANALL, but I think so. Why not? A constructor is just a function (member).

What is the use of such a constructor?

- . , , . , , (C), - ". .

#include <iostream>
#include <cstdarg>
struct Test
{
    Test(int n,...)
    {
        va_list va;
        va_start(va, n);
        for (int i = 0; i < n; ++i) {
             char const *s = va_arg(va, char*);
             std::cout<<"s=" << s << std::endl;
        }
        va_end(va);
    }
};
int main()
{
     Test t{3, "3","4","5"};
}

, "" . , "pure variadic", , , ++-. , , :

     Test t={"3","4","5", NULL};

Variadic?

" C, - ", . <cstdarg>, . , ++ 11, , , , / . ++ 98 .

+7

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


All Articles