Skip to content

Instantly share code, notes, and snippets.

@jasleon
Created February 6, 2024 19:15
Show Gist options
  • Save jasleon/1fcc0124c99cb1f8de8dba947b29891c to your computer and use it in GitHub Desktop.
Save jasleon/1fcc0124c99cb1f8de8dba947b29891c to your computer and use it in GitHub Desktop.
Variadic Template Exercise
// Challenge: Concatenate Strings
// This coding challenge involves creating a C++ class template called strOps.
// The goal is to concatenate a variable number of strings using variadic
// templates and fold expressions, count the total number of characters in the
// concatenated string, and keep track of the total number of input strings.
// Key Concepts:
// 1. Utilizing variadic templates to accept a variable number of input strings.
// 2. Implementing fold expressions to efficiently concatenate strings.
// 3. Utilizing variadic variable templates to count the total number of input
// strings.
// 4. Designing a class template with member functions to access the
// concatenated string, its length, and the count of input strings.
// 5. Creating a separate variadic function template (display_info) to display
// information about the concatenated string, character count, and string count.
// https://godbolt.org/z/G5chdPdGT
#include <iostream>
#include <string>
#include <string_view>
template <typename... Strings>
class strOps {
public:
// Constructor to concatenate strings and calculate information
explicit strOps(const Strings &...strings)
: concatenated_(concatenate(strings...)),
total_characters_(count_characters(concatenated_)) {}
std::string get_string() const { return concatenated_; }
std::size_t get_string_length() const { return total_characters_; }
std::size_t get_count() const { return total_strings_; }
private:
std::string concatenated_;
std::size_t total_characters_;
std::size_t total_strings_ =
count_<Strings...>; // sizeof...(Strings) is another option
// Counts total strings using variadic variable template
template <typename... T>
static constexpr std::size_t count_ =
(std::is_same_v<T, std::string> + ...);
// Helper function to concatenate strings using fold expression
template <typename... Args>
std::string concatenate(const Args &...args) const {
return (args + ...);
}
// Helper function to count total characters in a string
std::size_t count_characters(std::string_view sv) const {
return sv.length();
}
};
// Variadic function template to display information
template <typename... Ts>
void display_info(Ts... ts) {
((std::cout << ts), ...) << '\n';
}
int main() {
strOps<std::string, std::string, std::string, std::string, std::string>
strConcatenator("Hello ", "world", " in C++",
" template metaprogramming", "!");
display_info("String: ", strConcatenator.get_string());
display_info("Total characters: ", strConcatenator.get_string_length());
display_info("Number of strings merged: ", strConcatenator.get_count());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment