Skip to content

Commit

Permalink
Workaround for array<T,0> bug in MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
anarthal committed Jan 29, 2024
1 parent aee3750 commit 37165ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 23 additions & 0 deletions include/boost/mysql/detail/format_sql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <boost/mysql/detail/writable_field_traits.hpp>

#include <cstddef>
#include <type_traits>

namespace boost {
Expand Down Expand Up @@ -143,6 +144,28 @@ format_arg_descriptor make_format_arg_descriptor(const T& val)
return {make_format_value(val), {}};
}

// std::array<T, 0> with non-default-constructible T has bugs in MSVC prior to 14.3
template <std::size_t N>
struct format_arg_store
{
std::array<format_arg_descriptor, N> data;

template <class... Args>
format_arg_store(const Args&... args) noexcept : data{{make_format_arg_descriptor(args)...}}
{
}

span<const format_arg_descriptor> get() const noexcept { return data; }
};

template <>
struct format_arg_store<0u>
{
format_arg_store() = default;

span<const format_arg_descriptor> get() const noexcept { return {}; }
};

BOOST_MYSQL_DECL
void vformat_sql_to(
string_view format_str,
Expand Down
5 changes: 2 additions & 3 deletions include/boost/mysql/format_sql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ inline detail::format_arg_descriptor arg(string_view name, const T& value) noexc
template <BOOST_MYSQL_FORMATTABLE... Args>
inline std::string format_sql(string_view format_str, const format_options& opts, const Args&... args)
{
std::array<detail::format_arg_descriptor, sizeof...(Args)> desc{{detail::make_format_arg_descriptor(args
)...}};
detail::format_arg_store<sizeof...(Args)> store(args...);
format_context ctx(opts);
detail::vformat_sql_to(format_str, ctx, desc);
detail::vformat_sql_to(format_str, ctx, store.get());
return ctx.get().value();
}

Expand Down

0 comments on commit 37165ad

Please sign in to comment.