Variadic function without named argument

*爱你&永不变心* 提交于 2019-12-05 17:42:24

C++ Variadiac arguments are explained here. This syntax is supported in C++, but the arguments are not accessible:

In the C programming language, at least one named parameter must appear before the ellipsis parameter, so printz(...); is not valid.

In C++, this form is allowed even though the arguments passed to such function are not accessible, and is commonly used as the fallback overload in SFINAE, exploiting the lowest priority of the ellipsis conversion in overload resolution. This syntax for variadic arguments was introduced in 1987 C++ without the comma before the ellipsis. When C89 adopted function prototypes from C++, it replaced the syntax with one requiring the comma. For compatibility, C++98 accepts both C++-style f(int n...) and C-style f(int n, ...)

In C++ it is allowed because even if there is no named parameter before, the ... will only be an inaccessible variadic argument.

In C, there is no overloads, and having a function that receives only ... can be a great source of runtime error. Inaccessible varargs is not useful is C.

In C++, it is currently use as a sink function for sfinae. Compilers will always choose other overload if possible before resolving a call to a function with variadic parameter. It is pratical for sfinae purpose:

template<typename F, typename... Ts>
struct is_callable {
private:

    template<typename T, typename... Args>
    static decltype(
        static_cast<void>(std::declval<T>()(std::declval<Args>()...)),
        std::true_type{}
    ) test(int);

    template<typename...>
    static std::false_type test(...); // not a template variadic, classic vararg here.

public:
    // Here, the compiler will try the first version of the function
    // Because '...' is not the preferred overload
    // If the return type expression don't yield to a type, the compiler
    // will have no choice but to pick the variadic one,
    // resulting in a std::false_type
    using type = decltype(test<F, Ts...>(0));
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!