HolyBlackCat
HolyBlackCat

Reputation: 96719

Ellipsis as a constructor argument outside of any variadic templates

Consider the following code:

#include <iostream>

struct S
{
    S(const char *p) { std::cout << '[' << p << ']'; }
};

int main()
{
    S var(...); // <------
    return 0;
}

It compiles fine on GCC 5.2 with -pedantic -pedantic-errors, but prints nothing. I have no idea what this syntax means and I'm unable to find any information about it.

Looks like it just prevents an object from being constructed, but I've never heard about such feature.

The question is: What ellipsis means when used as a constructor argument?

Upvotes: 3

Views: 253

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

It should be a function prototype with variable-length arguments.

To make sure of it, I add two lines and got undefined reference error.

#include <iostream>

struct S
{
    S(const char *p) {std::cout << '[' << p << ']';}
};

int main()
{
    S var(...); // <------ function prototype declaration
    var(); // attempt to call the declared function, which is not defined
    var(1); // the same as above
    return 0;
}

Upvotes: 10

Related Questions