Demo entry 6759902

if constexpr

   

Submitted by cpp17 on Sep 05, 2018 at 08:44
Language: C++. Code size: 620 Bytes.

template<typename T> void linePrinter(const T& x)  {
    if constexpr (std::is_integral_v<T>)
        std::cout << "num: " << x << '\n';
    else if constexpr (std::is_floating_point_v<T>) {
        const auto frac = x - static_cast<long>(x);
        std::cout << "flt: " << x << ", frac " << frac << '\n';
    }
    else if constexpr(std::is_pointer_v<T>) {
        std::cout << "ptr, ";
        linePrinter(*x);
    }
    else
        std::cout << x << '\n'; 
}

template<typename ... Args> void printWithInfo(Args ... args) {
    (linePrinter(args), ...); // fold expression over the comma operator
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).