質問

In GManNickG's answer he used the notation:

template <typename>
static no& test(...);

Originally I thought this was just shorthand for "insert blah" and the user must substitute their own type:

template<typename>
static no& test(int);

However, after testing it in a compiler without any changes it worked! Additionally, overload resolution behaves as expected when using ... vs. an explicit int in the context of the original question.

What is this called in C++03/how should I interpret this code? It looks close to C++11's variadic templates, but there are some obvious differences between the two.

役に立ちましたか?

解決

Yes, ellipses specify a variadic function (variable length argument list). You can use the va_start, va_arg, and va_end macros to "pull" arguments out. You'll need an initial argument however; they need a starting place to begin grabbing arguments off of the stack (the arg before the ellipses + sizeof(arg)).

/* sum a bunch of ints */
int sum(int arg_cnt, ...) {
    va_list ap;
    va_start(ap, arg_cnt);
    int sum = 0;
    for(int i = 0; i < arg_cnt; ++i) {
        sum += va_arg(ap, int);
    }
    va_end(ap);
    return sum;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top