Skip to content

Instantly share code, notes, and snippets.

@Lazzlo2096
Created September 18, 2023 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lazzlo2096/23a6b65633d1a266d0f8bfae20f83ccc to your computer and use it in GitHub Desktop.
Save Lazzlo2096/23a6b65633d1a266d0f8bfae20f83ccc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
void print() {
cout << endl;
}
template <typename T>
void print(const T& t) {
cout << t << endl;
}
// Реализация для вывода вектора
template <typename T>
void print(const std::vector<T>& vec) {
std::cout << "[ ";
for (const auto& element : vec) {
std::cout << element << ", ";
}
std::cout << "]";
}
template <typename First, typename... Rest>
void print(const First& first, const Rest&... rest) {
// print(first)
cout << first << " ";
print(rest...); // recursive call using pack expansion syntax
}
int main()
{
print(); // calls first overload, outputting only a newline
print(1); // calls second overload
// these call the third overload, the variadic template,
// which uses recursion as needed.
print(10, 20);
print(100, 200, 300);
print("first", 2, "third", 3.14159);
std::vector<int> numbers = {1, 2, 3, 4, 5};
print(numbers);
print("Vector:", numbers);
print("Vector:", 232, numbers);
// print(numbers, numbers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment