Search code examples
c++templateslanguage-lawyervariadic-templatesvariadic-functions

function_traits with varidic tempate and va_args


Some time ago came across the use of something like this:

template <typename Return, typename ... Args>
struct function_traits<Return(*)(Args......)>{};

Where the above is the variadic expansion of Args, followed by a C style varidic.

Recently on another medium I was discussing this and the other party believed it was still valid to do this:

template <typename Return, typename ... Args>
struct function_traits<Return(*)(Args...,...)>{};

They believed the comma was allowed. But in the past I was forced to use ......, but I don't remember why. It absolutely is on stackoverflow, but it was on an unrelated topic and I couldn't find it again. I do remember something about a MSVC bug which might have forced me to omit the comma.

The question:

  1. Are both forms of the varidic expansion valid in that context, if not why?; or
  2. Does anybody know where the discussion I remember reading is?; in which case closing the topic then linking it here will help others find the answer. I was no longer able to find it so I assume others will have the same issue.

Solution

  • Are both forms of the varidic expansion valid in that context, if not why?

    Both forms are indeed valid. The relevant part of grammar production is at [dcl.fct]/3

    parameter-declaration-clause:
        parameter-declaration-listopt ...opt
        parameter-declaration-list , ... 

    Either way, parameter-declaration-list can be followed to a production that forms a template parameter pack expansion (that's where the second set of would ... come from).