Skip to content

Instantly share code, notes, and snippets.

@gintenlabo
Last active December 20, 2015 10:49
Show Gist options
  • Select an option

  • Save gintenlabo/6118950 to your computer and use it in GitHub Desktop.

Select an option

Save gintenlabo/6118950 to your computer and use it in GitHub Desktop.
#include <boost/utility/string_ref.hpp>
#include <utility>
#include <iostream>
std::size_t all_length(std::initializer_list<boost::string_ref> ss) {
std::size_t n = 0;
for (auto const& s : ss) {
n += s.size();
}
return n;
}
template<class... Args,
// SFINAE
class = decltype(
std::initializer_list<boost::string_ref>{std::declval<Args>()...})
>
std::size_t all_length(Args&&... args) {
return ::all_length({std::forward<Args>(args)...});
}
int main() {
std::string s = "hoge";
std::cout << all_length({}) << std::endl;
std::cout << all_length({"hoge"}) << std::endl;
std::cout << all_length() << std::endl;
std::cout << all_length("hoge") << std::endl;
std::cout << all_length("foo", s, "gonyogonyo") << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment