Skip to content
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

Approx::operator() not const-correct #2273

Closed
rupertnash opened this issue Aug 24, 2021 · 2 comments
Closed

Approx::operator() not const-correct #2273

rupertnash opened this issue Aug 24, 2021 · 2 comments
Labels

Comments

@rupertnash
Copy link

Describe the bug

The Approx type has an overload of template <typename T, ...SFINAE...> Approx operator()(T const&) which (correct me if I'm wrong) is meant to be a factory function for instances that have the same epsilon, margin, and scale, but that use the passed value.

AFAICT this should be const on the instance, but it's not.

Minimum failing example:

#include <catch2/catch.hpp>

TEST_CASE("Approx factory is const-correct") {
  // Set up a template Approx with problem-specific margin, etc.
  Approx const apprx = Approx(0.0).margin(1e-6);
  double value = 1.0;
  // Use template in assertions
  REQUIRE(value == apprx(1.0));
}

Expected behavior
Above test compiles, runs and passes.

Reproduction steps
See above.

Platform information:

  • OS: RHEL 8
  • Compiler+version: GCC 8.2.0
  • Catch version: 2.13.6

Additional context
Add any other context about the problem here.

@rupertnash
Copy link
Author

Sorry forgot to add error message:

$ g++ -I$Catch2_DIR/include const-approx.cpp 
In file included from const-approx.cpp:1:
const-approx.cpp: In function ‘void ____C_A_T_C_H____T_E_S_T____0()’:
const-approx.cpp:6:29: error: no match for call to ‘(const Catch::Detail::Approx) (double)’
   REQUIRE(value == apprx(1.0));
                             ^
In file included from const-approx.cpp:1:
$Catch2_DIR/include/catch2/catch.hpp:3094:16: note: candidate: ‘Catch::Detail::Approx Catch::Detail::Approx::operator()(const T&) [with T = double; <template-parameter-1-2> = void]’ <near match>
         Approx operator()( T const& value ) {
                ^~~~~~~~
$Catch2_DIR/include/catch2/catch.hpp:3094:16: note:   passing ‘const Catch::Detail::Approx*’ as ‘this’ argument discards qualifiers

@horenmar
Copy link
Member

Makes sense to have it const-qualified.

If you want the fix to be in v2, PRs are welcome 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants