## What happens if I call a variadic function with no arguments?

When you call a variadic function with no arguments, the parameter pack will be empty, and the size of the pack (sizeof...(Args)) will be zero. Depending on how the variadic function is implemented, this may or may not be a valid useÂ case.

Let's consider a fewÂ examples:

Example 1: Variadic function that expects at least oneÂ argument:

#include <iostream>

template <typename T, typename... Types>
void PrintValues(T First, Types... Rest) {
std::cout << First;
if constexpr (sizeof...(Rest) > 0) {
std::cout << ", ";
PrintValues(Rest...);
}
}

int main() {
PrintValues();  // Error: no matching function
}
error: 'PrintValues': no matching overloaded function found

In this case, calling PrintValues() with no arguments will result in a compilation error because the function expects at least one argument (T First).

Example 2: Variadic function that handles zeroÂ arguments:

#include <iostream>

template <typename... Types>
void PrintValues(Types... Args) {
if constexpr (sizeof...(Args) > 0) {
(std::cout << ... << Args) << '\n';
} else {
std::cout << "No arguments passed\n";
}
}

int main() {
// Valid: prints "No arguments passed"
PrintValues();

// Valid: prints "123"
PrintValues(1, 2, 3);
}
No arguments passed
123

In this example, the variadic function PrintValues() handles the case when no arguments are passed using an if constexpr statement. If the size of Args is greater than zero, it prints the arguments using a fold expression. Otherwise, it prints a special message indicating that no arguments wereÂ passed.

Example 3: Variadic function with a default empty baseÂ case:

#include <iostream>

void PrintValues() {
std::cout << "\nNo arguments passed\n"; }

template <typename T, typename... Types>
void PrintValues(T First, Types... Rest) {
std::cout << First;
if constexpr (sizeof...(Rest) > 0) {
std::cout << ", ";
}
PrintValues(Rest...);
}

int main() {
// Valid: calls the non-template overload
PrintValues();

// Valid: calls the variadic template
PrintValues(1, 2, 3);
}
No arguments passed
1, 2, 3
No arguments passed

In this approach, we provide a non-template overload of PrintValues() that takes no arguments and serves as the base case for the variadic template. When PrintValues() is called with no arguments, it invokes the non-template overload, which prints a special message. When called with arguments, it invokes the variadic template, which recursively prints theÂ arguments.

In summary, calling a variadic function with no arguments is valid if the function is designed to handle that case, either through compile-time conditionals or by providing a non-template base case. If the variadic function expects at least one argument, calling it with no arguments will result in a compilationÂ error.

This Question is from the Lesson:

An introduction to variadic functions, which allow us to pass a variable quantity of arguments

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

An introduction to variadic functions, which allow us to pass a variable quantity of arguments

Part of the course:

## Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

### This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

### Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.