Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
New C++ Insights Episode
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <iostream> #include <string> #include <type_traits> namespace traits { namespace impl // a "private" implementation { // generic type is not a string... template <typename AnyType> inline constexpr bool isString = false; // ... but these types are strings template <> inline constexpr bool isString<std::string> = true; template <> inline constexpr bool isString<std::string_view> = true; template <> inline constexpr bool isString<char *> = true; template <> inline constexpr bool isString<const char *> = true; } // isString<T> = impl::isString<T>, but with dropped const/volatile on T: template <typename T> inline constexpr bool isString = impl::isString<std::remove_cv_t<T>>; } template <typename Numeric> auto makeString(Numeric value) -> decltype(std::to_string(value)) { return std::to_string(value); } // will fail substitution if `traits::isString<Iterable>` // or when can't evaluate the type of makeString(*begin(container)) template <typename Iterable> auto makeString(const Iterable& iterable) -> std::enable_if_t< !traits::isString<Iterable>, decltype(makeString(*std::begin(iterable))) > { std::string result; for (const auto& i : iterable) { if (!result.empty()) result += ';'; result += makeString(i); } return result; } // will fail substituition if `!traits::isString<String>` template <typename String> auto makeString(const String& s) -> std::enable_if_t< traits::isString<String>, std::string > { return std::string(s); } int main() { std::cout << makeString("Hello, ") << makeString(std::string_view("world")) << makeString(std::string("!!1")) << std::endl; }
Insight:
Console: