diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index 09155f2..1c985a3 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -120,9 +120,14 @@ class parser { } template - auto on_error(Fun fun) { + auto on_error(Fun&& fun) { if (!parser_.valid()) { - fun(parser_.error_msg()); + if constexpr (std::is_invocable_v) { + fun(); + } else { + std::invoke(std::forward(fun), + parser_.error_msg()); + } } return *this; } diff --git a/test/test_parser.cpp b/test/test_parser.cpp index 7e1ba61..67798a5 100644 --- a/test/test_parser.cpp +++ b/test/test_parser.cpp @@ -189,7 +189,6 @@ TEST_CASE("testing composite conversion") { ss::parser p{f.name, ","}; auto fail = [] { FAIL(""); }; - auto fail_if_error = [](auto& error) { CHECK(error.empty()); }; auto expect_error = [](auto error) { CHECK(!error.empty()); }; REQUIRE(p.valid()); @@ -203,7 +202,7 @@ TEST_CASE("testing composite conversion") { .or_else(fail) .or_else( [](auto&& data) { CHECK(data == expectedData); }) - .on_error(fail_if_error) + .on_error(fail) .or_else(fail) .values(); @@ -224,11 +223,11 @@ TEST_CASE("testing composite conversion") { [](auto& i1, auto i2, double d) { CHECK(std::tie(i1, i2, d) == expectedData); }) - .on_error(fail_if_error) + .on_error(fail) .or_else_object(fail) - .on_error(fail_if_error) + .on_error(fail) .or_else(fail) - .on_error(fail_if_error) + .on_error(fail) .or_else(fail) .values(); @@ -313,9 +312,9 @@ TEST_CASE("testing composite conversion") { REQUIRE(!p.eof()); auto [d1, d2] = p.try_next>() - .on_error(fail_if_error) + .on_error(fail) .or_else>(fail) - .on_error(fail_if_error) + .on_error(fail) .values(); REQUIRE(p.valid()); @@ -329,9 +328,9 @@ TEST_CASE("testing composite conversion") { auto [d1, d2] = p.try_next>() - .on_error(fail_if_error) + .on_error(fail) .or_else>(fail) - .on_error(fail_if_error) + .on_error(fail) .values(); REQUIRE(p.valid());