Skip to content
Snippets Groups Projects

Add NaNHelper utility

Merged Stéphane Del Pino requested to merge feature/nan-output-for-vectors-and-matrices into develop
1 file
+ 38
0
Compare changes
  • Side-by-side
  • Inline
  • 75e9b183
    Add NaNHelper utility · 75e9b183
    Stéphane Del Pino authored
    The aim of this helper is to allow to print signaling NaN.
    This should help to debug uninitialized matrices of vectors values
    while printing them.
+ 38
0
#ifndef NAN_HELPER_HPP
#define NAN_HELPER_HPP
#include <cmath>
#include <iostream>
#include <type_traits>
template <typename T>
class NaNHelper
{
private:
const T& m_t;
public:
friend std::ostream&
operator<<(std::ostream& os, const NaNHelper<T>& helper)
{
if constexpr (std::is_arithmetic_v<T>) {
if (std::isnan(helper.m_t)) {
os << "nan";
} else {
os << helper.m_t;
}
} else {
os << helper.m_t;
}
return os;
}
NaNHelper(const T& t) : m_t{t} {}
NaNHelper(NaNHelper&&) = delete;
NaNHelper(const NaNHelper&) = delete;
NaNHelper() = delete;
~NaNHelper() = default;
};
#endif // NAN_HELPER_HPP
Loading