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
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
New C++ Insights Episode
×
Sponsors:
Made by
Andreas Fertig
Source:
//clang 6.0.0 #include <iostream> #include <utility> #include <type_traits> using namespace std::literals; class Ball { public: template <typename T, typename T1> static constexpr auto best_match(T1 const& a, T const& b) { return b; } template <typename T, typename T2> static constexpr auto best_match(T const& a, T2 const& b) { return a; } template <typename T1 = std::string , typename T2 = std::conditional_t<std::is_convertible_v<T1, std::string>, double, std::string> , typename = std::enable_if_t< (std::is_convertible_v<T1, std::string> && std::is_convertible_v<T2, double>) || (std::is_convertible_v<T2, std::string> && std::is_convertible_v<T1, double>) > > Ball(T1 f = best_match<T1>("red"s, 20.0), T2 s = best_match<T2>("red"s, 20.0)) : m_color(best_match<std::string>(f, s)), m_radius(best_match<double>(f, s)) { } std::string const& color() const { return m_color; } double radius() const { return m_radius; } private: std::string m_color; double m_radius; }; std::ostream& operator<<(std::ostream& out, Ball const& ball) { return out << "c: " << ball.color() << " r: " << ball.radius(); } int main() { std::cout << Ball(20.0, "red"s) << std::endl; std::cout << Ball("red"s) << std::endl; std::cout << Ball(20.0) << std::endl; std::cout << Ball("red"s, 20.0) << std::endl; std::cout << Ball() << std::endl; }
Insight:
Console: