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
None
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
// --- #include "makeString.hpp" --- #include <string> #include <type_traits> namespace Impl { template <typename NotString> inline constexpr bool isString = false; template <> inline constexpr bool isString<std::string> = true; template <> inline constexpr bool isString<char*> = true; template <> inline constexpr bool isString<const char*> = true; template <> inline constexpr bool isString<const char* const> = true; template <> inline constexpr bool isString<std::string_view> = true; } template <typename Object> auto makeString(const Object &object) -> decltype(object.to_string()) { return object.to_string(); } template <typename Numeric> auto makeString(Numeric value) -> decltype(std::to_string(value)) { return std::to_string(value); } template <typename Iterable> auto makeString(const Iterable &iterable) -> std::enable_if_t<!Impl::isString<Iterable>, decltype(makeString(*std::begin(iterable)))> { std::string result; for (const auto &i : iterable) { if (!result.empty()) result += ';'; result += makeString(i); } return result; } template <typename String> auto makeString(const String& s) -> std::enable_if_t<Impl::isString<String>, std::string> { return std::string(s); } // ----------- // main.cpp extern void dontOptimizeOut(const std::string&); int main() { dontOptimizeOut(makeString("Hello, ")); }
Insight:
Console: