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
Workshop: Safe and Efficient C++ for Embedded Environments
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <cmath> #include <iostream> class Complex { public: constexpr Complex(double r, double i) : real_(r), imag_(i) {} Complex(double ri[2]) : real_(ri[0]), imag_(ri[1]) {} friend Complex sqrt(const Complex &z); friend constexpr Complex operator*(const Complex &x, const Complex &y); private: double real_, imag_; }; constexpr Complex operator*(const Complex &x, const Complex &y) { return {(x.real_ * y.real_), (x.imag_ * y.imag_)}; }; #define CUBE(X) ((X) * (X) * (X)) constexpr Complex cubeme(const Complex &x) { return x * x * x; } Complex sqrt(const Complex &z) { double coeffs[]{0.0, 0.0}; if ((0.0 == z.imag_) && (z.real_ < 0)) { return coeffs; } double r = ::std::sqrt((z.real_ * z.real_) + (z.imag_ * z.imag_)); if (r != 0.0) { double theta = asin(z.imag_ / r); coeffs[0] = ::std::sqrt(r) * cos(theta / 2.0); coeffs[1] = ::std::sqrt(r) * sin(theta / 2.0); } return coeffs; } void stupidmacro(void) { Complex cn(27.0, 13.5); CUBE(sqrt(cn)); } void constexprversion(void) { Complex cn(27.0, 13.5); cubeme(sqrt(cn)); }
Insight:
Console: