Skip to content

Make unexpected types in quote variant a compile-time error #2225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@
<ClInclude Include="ql\utilities\observablevalue.hpp" />
<ClInclude Include="ql\utilities\steppingiterator.hpp" />
<ClInclude Include="ql\utilities\tracing.hpp" />
<ClInclude Include="ql\utilities\variants.hpp" />
<ClInclude Include="ql\utilities\vectors.hpp" />
<ClInclude Include="ql\any.hpp" />
<ClInclude Include="ql\auto_link.hpp" />
Expand Down
3 changes: 3 additions & 0 deletions QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,9 @@
<ClInclude Include="ql\utilities\tracing.hpp">
<Filter>utilities</Filter>
</ClInclude>
<ClInclude Include="ql\utilities\variants.hpp">
<Filter>utilities</Filter>
</ClInclude>
<ClInclude Include="ql\utilities\vectors.hpp">
<Filter>utilities</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions ql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,7 @@ set(QL_HEADERS
utilities/observablevalue.hpp
utilities/steppingiterator.hpp
utilities/tracing.hpp
utilities/variants.hpp
utilities/vectors.hpp
version.hpp
volatilitymodel.hpp
Expand Down
13 changes: 4 additions & 9 deletions ql/quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@
#include <ql/quote.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/errors.hpp>
#include <ql/utilities/variants.hpp>

namespace QuantLib {

Handle<Quote> handleFromVariant(const std::variant<Real, Handle<Quote>>& value) {
return std::visit(
[](const auto& x) -> Handle<Quote> {
using T = std::decay_t<decltype(x)>;
if constexpr (std::is_same_v<T, Real>) {
return makeQuoteHandle(x);
} else if constexpr (std::is_same_v<T, Handle<Quote>>) {
return x;
} else {
QL_FAIL("Unexpected type in quote variant");
}
detail::variant_visitor{
[](Real x) -> Handle<Quote> { return makeQuoteHandle(x); },
[](const Handle<Quote>& x) { return x; }
},
value);
}
Expand Down
3 changes: 2 additions & 1 deletion ql/utilities/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ this_include_HEADERS = \
dataformatters.hpp \
dataparsers.hpp \
null.hpp \
null_deleter.hpp \
null_deleter.hpp \
observablevalue.hpp \
steppingiterator.hpp \
tracing.hpp \
variants.hpp \
vectors.hpp

cpp_files = \
Expand Down
1 change: 1 addition & 0 deletions ql/utilities/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
#include <ql/utilities/observablevalue.hpp>
#include <ql/utilities/steppingiterator.hpp>
#include <ql/utilities/tracing.hpp>
#include <ql/utilities/variants.hpp>
#include <ql/utilities/vectors.hpp>

19 changes: 19 additions & 0 deletions ql/utilities/variants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#ifndef quantlib_utilities_variants_hpp
#define quantlib_utilities_variants_hpp

namespace QuantLib::detail {

// Helper type for use with std::visit.
template <class... Ts>
struct variant_visitor : Ts... {
using Ts::operator()...;
};

template <class... Ts>
variant_visitor(Ts...) -> variant_visitor<Ts...>;

}

#endif
Loading