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

[REVIEW] Add explicit conversion for fixed_point to bool. #5859

Merged
merged 4 commits into from
Aug 6, 2020

Conversation

bdice
Copy link
Contributor

@bdice bdice commented Aug 5, 2020

This PR adds an explicit conversion template specialization from fixed_point values to bool. Below, I explain the problem this is solving in case it's helpful for someone in the future.

While developing the AST interpreter (#5494), I implemented a logical "and" operator. I noticed that the expression fixed_point_value && true tries to convert fixed_point to a bool with this cast operator but fails to compile (bool passes the is_integral trait, and is thus an allowed cast by the is_supported_construction_value trait).

The compilation failure is in left_shift (it can't compile this line, which tries to multiply the cast-to type bool by the scale factor. You might wonder why this is happening -- the conversion is marked explicit. The cause is that logical operators perform explicit conversions [1, 2]. References for this behavior are below.

[1] https://en.cppreference.com/w/cpp/language/operator_logical (bold added)

If the operand is not bool, it is converted to bool using contextual conversion to bool: it is only well-formed if the declaration bool t(arg) is well-formed, for some invented temporary t.

[2] https://en.cppreference.com/w/cpp/language/implicit_conversion (under "Contextual conversions", bold added)

In the following contexts, the type bool is expected and the implicit conversion is performed if the declaration bool t(e); is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.

  • the controlling expression of if, while, for;
  • the operands of the built-in logical operators !, && and ||;
  • the first operand of the conditional operator ?:;
  • the predicate in a static_assert declaration;
  • the expression in a noexcept specifier;

@bdice bdice requested a review from a team as a code owner August 5, 2020 23:01
@bdice bdice self-assigned this Aug 5, 2020
@bdice bdice requested review from jrhemstad, rgsl888prabhu and codereport and removed request for rgsl888prabhu August 5, 2020 23:01
@GPUtester
Copy link
Collaborator

Please update the changelog in order to start CI tests.

View the gpuCI docs here.

@bdice bdice added 3 - Ready for Review Ready for review by team libcudf Affects libcudf (C++/CUDA) code. labels Aug 5, 2020
Copy link
Contributor

@codereport codereport left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for the fix! Only small change to Doxygen comment

cpp/include/cudf/fixed_point/fixed_point.hpp Outdated Show resolved Hide resolved
cpp/include/cudf/fixed_point/fixed_point.hpp Outdated Show resolved Hide resolved
@codereport codereport changed the title Add explicit conversion for fixed_point to bool. [REVIEW] Add explicit conversion for fixed_point to bool. Aug 5, 2020
Co-authored-by: Conor Hoekstra <36027403+codereport@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Aug 6, 2020

Codecov Report

Merging #5859 into branch-0.15 will increase coverage by 0.32%.
The diff coverage is n/a.

Impacted file tree graph

@@               Coverage Diff               @@
##           branch-0.15    #5859      +/-   ##
===============================================
+ Coverage        84.10%   84.43%   +0.32%     
===============================================
  Files               80       80              
  Lines            13094    13403     +309     
===============================================
+ Hits             11013    11317     +304     
- Misses            2081     2086       +5     
Impacted Files Coverage Δ
python/cudf/cudf/io/feather.py 100.00% <0.00%> (ø)
python/cudf/cudf/comm/serialize.py 0.00% <0.00%> (ø)
python/custreamz/custreamz/_version.py 0.00% <0.00%> (ø)
python/dask_cudf/dask_cudf/_version.py 0.00% <0.00%> (ø)
python/dask_cudf/dask_cudf/io/tests/test_orc.py 100.00% <0.00%> (ø)
python/dask_cudf/dask_cudf/io/tests/test_json.py 100.00% <0.00%> (ø)
...ython/dask_cudf/dask_cudf/io/tests/test_parquet.py 100.00% <0.00%> (ø)
python/cudf/cudf/utils/applyutils.py 98.75% <0.00%> (+0.02%) ⬆️
python/cudf/cudf/core/join/join.py 92.41% <0.00%> (+0.03%) ⬆️
...ython/custreamz/custreamz/tests/test_dataframes.py 98.80% <0.00%> (+0.03%) ⬆️
... and 29 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 120c871...bfe17b4. Read the comment docs.

@jrhemstad jrhemstad merged commit c86d76c into rapidsai:branch-0.15 Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 - Ready for Review Ready for review by team libcudf Affects libcudf (C++/CUDA) code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants