QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
null.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004 StatPro Italia srl
6 Copyright (C) 2010 Kakhkhor Abdijalilov
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file null.hpp
23 \brief null values
24*/
25
26#ifndef quantlib_null_hpp
27#define quantlib_null_hpp
28
29#include <ql/types.hpp>
30#include <type_traits>
31#include <limits>
32
33namespace QuantLib {
34
35 #ifdef QL_NULL_AS_FUNCTIONS
36
37 //! template function providing a null value for a given type.
38 template <typename T>
39 T Null() {
40 if constexpr (std::is_floating_point_v<T>) {
41 // a specific, unlikely value that should fit into any Real
42 return (std::numeric_limits<float>::max)();
43 } else if constexpr (std::is_integral_v<T>) {
44 // this should fit into any Integer
45 return (std::numeric_limits<int>::max)();
46 } else {
47 return T();
48 }
49 }
50
51 #else
52
53 //! template class providing a null value for a given type.
54 template <class Type>
55 class Null;
56
57 // default implementation for built-in types
58 template <typename T>
59 class Null {
60 public:
61 Null() = default;
62 operator T() const {
63 if constexpr (std::is_floating_point_v<T>) {
64 // a specific, unlikely value that should fit into any Real
65 return (std::numeric_limits<float>::max)();
66 } else if constexpr (std::is_integral_v<T>) {
67 // this should fit into any Integer
68 return (std::numeric_limits<int>::max)();
69 } else {
70 return T();
71 }
72 }
73 };
74
75 #endif
76
77}
78
79
80#endif
template class providing a null value for a given type.
Definition: null.hpp:59
Null()=default
Definition: any.hpp:37
Custom types.