Skip to content

Commit

Permalink
Fix compilation error when using more than 11 arguments with scn::scan
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed May 18, 2024
1 parent ff77a65 commit 12d503c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _Released 2024-05-xx_
* Fix documentation: default format type specifier for integers is `i`, not `d`:
when not explicitly specified by a format specifier, the base of an integer is determined based on its prefix:
`0x...` is hexadecimal, `0...` or `0o...` is octal, `0b...` is binary, and everything else is decimal.
* Fix a compilation error which would occur when scanning more than 11 arguments with `scn::scan`.
* Small CMake adjustments to better support use as a subproject (#113, thanks [@frankmiller (Frank Miller)](https://github.com/frankmiller))
* Fix misplaced include of `GNUInstallDirs` in CMake (#111, thanks [@WangWeiLin-MV](https://github.com/WangWeiLin-MV))
* Allow for externally installed versions for GTest and Google Benchmark (#112, thanks [@xvitaly (Vitaly)](https://github.com/xvitaly))
Expand Down
15 changes: 10 additions & 5 deletions include/scn/detail/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class arg_value {
SCN_STD > SCN_STD_20
constexpr
#endif
arg_value() = default;
arg_value() = default;

template <typename T>
explicit constexpr arg_value(T& val) : ref_value{std::addressof(val)}
Expand Down Expand Up @@ -333,9 +333,14 @@ constexpr size_t encode_types_impl()
template <typename CharT, typename... Ts>
constexpr size_t encode_types()
{
static_assert(sizeof...(Ts) < (1 << packed_arg_bits));
return sizeof...(Ts) |
(encode_types_impl<CharT, Ts...>() << packed_arg_bits);
if constexpr (sizeof...(Ts) < (1 << packed_arg_bits)) {
return sizeof...(Ts) |
(encode_types_impl<CharT, Ts...>() << packed_arg_bits);
}
else {
SCN_EXPECT(false);
SCN_UNREACHABLE;
}
}

template <typename Arg>
Expand Down Expand Up @@ -695,7 +700,7 @@ class basic_scan_args {
: m_desc{desc}, m_values{values}
{
}
constexpr basic_scan_args(size_t desc, basic_scan_args<Context>* args)
constexpr basic_scan_args(size_t desc, basic_scan_arg<Context>* args)
: m_desc{desc}, m_args{args}
{
}
Expand Down
27 changes: 27 additions & 0 deletions tests/unittests/scan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,30 @@ TEST(ScanTest, DeconstructedTimestamp2)
EXPECT_EQ(std::get<4>(res->values()), 20);
EXPECT_STREQ(res->range().data(), "33.576864");
}

TEST(ScanTest, LotsOfArguments)
{
auto res = scn::scan<int, int, int, int, int, int, int, double>(
"1 2 3 4 5 6 7 8.9", "{} {} {} {} {} {} {} {}");
ASSERT_TRUE(res);
auto [a1, a2, a3, a4, a5, a6, a7, a8] = res->values();
EXPECT_EQ(a1, 1);
EXPECT_EQ(a2, 2);
EXPECT_EQ(a3, 3);
EXPECT_EQ(a4, 4);
EXPECT_EQ(a5, 5);
EXPECT_EQ(a6, 6);
EXPECT_EQ(a7, 7);
EXPECT_DOUBLE_EQ(a8, 8.9);
}
TEST(ScanTest, EvenMoreArguments)
{
auto res = scn::scan<int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int>(
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 "
"27 28 29 30 31 32 33",
"{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} "
"{} {} {} {} {} {} {} {} {} {}");
ASSERT_TRUE(res);
}

0 comments on commit 12d503c

Please sign in to comment.