Use C++17 fold expression:
template<class... Args>
constexpr auto total_sum(const Args&... args) {
return (args + ... + 0);
}
static_assert(total_sum(1, 2, 5, 4, 2) == 14);
static_assert(total_sum(3, 5, 6, 2) == 16);
More Related Contents:
- How to “Reveal in Finder” or “Show in Explorer” with Qt
- How to use new std::byte type in places where old-style unsigned char is needed?
- ++i or i++ in for loops ?? [duplicate]
- Check if one string is a prefix of another
- Generating one class member per variadic template argument
- Copying derived entities using only base class pointers, (without exhaustive testing!) – C++
- How to initialize all elements in an array to the same number in C++ [duplicate]
- Expand macro inside string literal
- Windows & C++: extern & __declspec(dllimport)
- Why is `make_unique` disallowed?
- Type erasure in C++: how boost::shared_ptr and boost::function work?
- How can I copy one map into another using std::copy?
- Compile-time map and inverse map values
- Calling erase with iterator vs const_iterator
- Set the digits after decimal point
- prolonging the lifetime of temporaries
- What is default storage class for global variables?
- C++ Implicit Conversion (Signed + Unsigned)
- Standard way to build a Chrome extension into Chromium
- Parse config file in C/C++
- C++ Get Handle of Open Sockets of a Program
- C++11: “narrowing conversion inside { }” with modulus
- parsing into several vector members
- Area of rectangle-rectangle intersection
- Same class name in different C++ files
- Write concurrently vector
- Complete example using Boost::Signals for C++ Eventing
- List of common C++ Optimization Techniques [closed]
- Why does a virtual function get hidden?
- Implementing the visitor pattern using C++ Templates