Variadic Functions

Zero Argument Variadic Functions

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

Abstract art representing computer programming

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.

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

A computer programmer
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:

  • 125 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.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved