Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In this compliant solution, a variadic function using a function parameter pack is used to implement the Add() function, allowing identical behavior for call sites. Unlike the C-style variadic function used in the noncompliant code example, this compliant solution does not result in undefined behavior if the list of parameters is not terminated with 0. Additionally, if any of the values passed to the function are not an integerintegers, it results in the code being becomes ill-formed instead of rather than producing undefined behavior.

Code Block
bgColor#ccccff
langcpp
#include <type_traits>
 
template <typename Arg, typename std::enable_if<std::is_integral<Arg>::value>::type * = nullptr>
int Add(Arg F, Arg S) { return F + S; }
 
template <typename Arg, typename... Ts, typename std::enable_if<std::is_integral<Arg>::value>::type * = nullptr>
int Add(Arg F, Ts... Rest) {
  return F + Add(Rest...);
}

...