This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Properly convert the count arguments to the *_n algorithms before use.
ClosedPublic

Authored by EricWF on Feb 5 2015, 2:57 PM.

Details

Summary

The requirement on the Size type passed to *_n algorithms is that it is convertible to an integral type. This means we can't use a variable of type Size directly. Instead we need to convert it to an integral type first. The problem is finding out what integral type to convert it to. __convert_to_integral figures out what integral type to convert it to and performs the conversion, It also promotes the resulting integral type so that it is at least as big as an integer. __convert_to_integral also has a special case for converting enums. This should only work on non-scoped enumerations because it does not apply an explicit conversion from the enum to its underlying type.

Diff Detail

Repository
rL LLVM

Event Timeline

EricWF updated this revision to Diff 19435.Feb 5 2015, 2:57 PM
EricWF retitled this revision from to [libcxx] Properly convert the count arguments to the *_n algorithms before use..
EricWF updated this object.
EricWF edited the test plan for this revision. (Show Details)
EricWF added reviewers: mclow.lists, chandlerc.
EricWF added a subscriber: Unknown Object (MLST).
mclow.lists edited edge metadata.Feb 9 2015, 12:47 PM

A few nits; but in general, this looks fine to me

include/algorithm
1675 ↗(On Diff #19435)

I don't think you need a _VSTD:: in front of __convert_to_integral, since it's not a legal identifier, so people can't define their own.

include/type_traits
3663 ↗(On Diff #19435)

Do you need cases for signed char and unsigned char ?

test/libcxx/type_traits/convert_to_integral.pass.cpp
73 ↗(On Diff #19435)

As long as you're modifying this file, please add a newline at the end.

test/std/algorithms/alg.modifying.operations/alg.copy/copy_n.pass.cpp
21 ↗(On Diff #19435)

Rather than having this IntegralConvertible in several source files, should it go into test/support?

(suggestion, not requirement)

EricWF updated this revision to Diff 19683.Feb 10 2015, 8:05 AM
EricWF edited edge metadata.

Address @mclow.lists comments.

Add inline comments.

include/algorithm
1675 ↗(On Diff #19435)

Acknowledged.

include/type_traits
3663 ↗(On Diff #19435)

I thought you might if char was signed char and you wanted to convert unsigned char x = -1; but it actually seems you don't need the char overload at all.

I've updated the tests to check the conversion of both the min and max values for each type and I've removed the char overload.

test/libcxx/type_traits/convert_to_integral.pass.cpp
73 ↗(On Diff #19435)

Acknowledged.

test/std/algorithms/alg.modifying.operations/alg.copy/copy_n.pass.cpp
21 ↗(On Diff #19435)

Acknowledged.

mclow.lists accepted this revision.Feb 10 2015, 8:29 AM
mclow.lists edited edge metadata.

LGTM

This revision is now accepted and ready to land.Feb 10 2015, 8:29 AM
EricWF closed this revision.Feb 10 2015, 8:48 AM
This revision was automatically updated to reflect the committed changes.