4

Consider following program:

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

I think it is variadic constructor. Does the C++ standard says that constructor can be variadic? What is the use of such constructor? What is the rationale for allowing variadic constructor?

Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 5
    So you don't have to write multiple constructors. This one isn't very useful, by the way, it doesn't provide any info as to what the parameters actually are. – Mr Lister Oct 13 '15 at 16:45
  • 4
    For the first question: functions can be variadic, constructor is a function. Why can't it be variadic? – lisyarus Oct 13 '15 at 16:45
  • 1
    @MrLister: It would be better if you write an answer that explains its usefulness using an example. – Destructor Oct 13 '15 at 16:46
  • 9
    Now that we have variadic templates, the varargs method is pretty much useless. Lack of type safety and all. – Ben Voigt Oct 13 '15 at 16:47
  • 2
    Pravasi Meet, but as @lisyarus says, a variadic constructor is exactly as useful as any other variadic function. Are you really inquiring about what variadic functions are for at all? – Mr Lister Oct 13 '15 at 16:51

1 Answers1

7

Let's try to answer your questions one by one:

I think it is variadic constructor.

You are correct.

Does the C++ standard says that constructor can be variadic?

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

What is the use of such constructor?

Like any other variadic function - to pass a variable number of arguments. It also has the same problems, mostly no type safety as any other variadic function. For example, let's say you need a list of (C) strings, you could do something like See live demo here.

#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"};
}

Keep in mind that for this to work, you need at least one "non-variadic" parameter. So a "pure variadic" constructor, like the one you show, doesn't make much sense in portable C++ code. For any particular platform, you may know how to access the parameters even without the non-variadic parameter, so, this might work:

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

What is the rationale for allowing variadic constructor?

"It's C compatible and someone might have a use of it", I guess. If you know your way around <cstdarg>, it can be an effective tool. Of course, with C++11, you should most probably use variadic templates / perfect forwarding and initialization lists instead. But, with C++98 you didn't have these tools.

Destructor
  • 14,123
  • 11
  • 61
  • 126
srdjan.veljkovic
  • 2,468
  • 16
  • 24