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

std::abs() not considered constexpr by clang #246

Open
holynski opened this issue Aug 21, 2019 · 1 comment
Open

std::abs() not considered constexpr by clang #246

holynski opened this issue Aug 21, 2019 · 1 comment

Comments

@holynski
Copy link
Contributor

When compiling a fresh clone on OSX 10.14.3:

src/theia/sfm/estimators/estimate_radial_distortion_homography_test.cc:73:25: error: constexpr variable 'kRadDistThreshold1' must be initialized by a constant
expression
static constexpr double kRadDistThreshold1 =
^
src/theia/sfm/estimators/estimate_radial_distortion_homography_test.cc:74:11: note: non-constexpr function 'abs' cannot be used in a constant expression
0.1 * std::abs(kRadialDistortion1 * std::pow(kFocalLength1, 2));
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/math.h:737:1: note: declared here
abs(double __lcpp_x) _NOEXCEPT {return ::fabs(__lcpp_x);}
^

Seems like Clang++ (v10.0) on OSX doesn't allow std::abs to be used in the definition of a constexpr. The same is true for std::pow().

I have a fix for this (see below), and have tried making a PR - but it looks like when I clone from GerritHub, it gives me a significantly older version of the repo (with most recent commit 14421f7), and this file didn't exist back then. I'm following the instructions on here, and cloning from this repo: https://review.gerrithub.io/sweeneychris/TheiaSfM. Did I make a mistake somewhere?

Original code:

// allowing 10 percent error for radial distortion parameters
static constexpr double kRadDistThreshold1 =
    0.1 * std::abs(kRadialDistortion1 * std::pow(kFocalLength1,2));
static constexpr double kRadDistThreshold2 =
    0.1 * std::abs(kRadialDistortion2 * std::pow(kFocalLength2,2));

The fix:

namespace {

  constexpr double ConstexprAbs(const double in) {
    return in >= 0 ? in : -in;
  }

}  // namespace

// allowing 10 percent error for radial distortion parameters
static constexpr double kRadDistThreshold1 =
    0.1 * ConstexprAbs(kRadialDistortion1 * kFocalLength1 * kFocalLength1);
static constexpr double kRadDistThreshold2 =
    0.1 * ConstexprAbs(kRadialDistortion2 * kFocalLength2 * kFocalLength2);
@holynski
Copy link
Contributor Author

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

No branches or pull requests

1 participant