diff --git a/include/sqlpp11/result_field_base.h b/include/sqlpp11/result_field_base.h index 6d33ae35..924ad69d 100644 --- a/include/sqlpp11/result_field_base.h +++ b/include/sqlpp11/result_field_base.h @@ -33,6 +33,7 @@ #include #include #include +#include namespace sqlpp { @@ -115,7 +116,9 @@ namespace sqlpp return _value; } - operator _cpp_value_type() const + operator typename std::conditional<_null_is_trivial or (not _can_be_null::value), + _cpp_value_type, + bad_statement>::type() const { return value(); } diff --git a/test_constraints/CMakeLists.txt b/test_constraints/CMakeLists.txt index 87e7d197..6787d988 100644 --- a/test_constraints/CMakeLists.txt +++ b/test_constraints/CMakeLists.txt @@ -33,6 +33,8 @@ function(test_constraint name pattern) set_property(TEST ${test} PROPERTY PASS_REGULAR_EXPRESSION ${pattern}) endfunction() +add_executable(see_what_happens no_conversion_operator_if_null_not_trivial.cpp) +target_link_libraries(see_what_happens PRIVATE sqlpp11 sqlpp11_testing) test_constraint(count_of_count "count\\(\\) cannot be used on an aggregate function") test_constraint(max_of_max "max\\(\\) cannot be used on an aggregate function") test_constraint(no_conversion_operator_if_null_not_trivial "int i = row.alpha") diff --git a/tests/Function.cpp b/tests/Function.cpp index 728b6c63..7f3e3a0d 100644 --- a/tests/Function.cpp +++ b/tests/Function.cpp @@ -35,7 +35,7 @@ SQLPP_ALIAS_PROVIDER(kaesekuchen) -int Function(int, char*[]) +int Function(int, char* []) { MockDb db = {}; const auto f = test::TabFoo{}; @@ -286,7 +286,7 @@ int Function(int, char*[]) static_assert(sqlpp::is_integral_t::value, "type requirement"); static_assert(not sqlpp::is_floating_point_t::value, "type requirement"); - if (false and db(select(count(t.alpha)).from(t).where(true)).front().count) + if (false and db(select(count(t.alpha)).from(t).where(true)).front().count > 0) { /* do something */ } } @@ -446,11 +446,14 @@ int Function(int, char*[]) for (const auto& row : db(select(all_of(t)).from(t).where(true))) { - static_assert(std::is_same::type>>::value, + static_assert(std::is_same::type>>::value, "result fields are accepted and not wrapped"); - static_assert(std::is_same::type>>::value, + static_assert(std::is_same::type>>::value, "result fields are accepted and not wrapped"); - static_assert(std::is_same::type>>::value, + static_assert(std::is_same::type>>::value, "result fields are accepted and not wrapped"); } }