[libcxx-commits] [PATCH] D98151: [libcxx] adds std::identity to <functional>

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Mar 7 17:44:59 PST 2021


Quuxplusone added inline comments.


================
Comment at: libcxx/test/std/utilities/function.objects/func.identity/identity.pass.cpp:43
+  return result;
+}
+
----------------
Quuxplusone wrote:
> Please, as @ldionne would say, "add some simple tests." :)
> I think given the simplicity of `std::identity`, the entire test suite for it should look like this:
> ```
> #include "support/moveonly.h"
> 
> constexpr bool test() {
>     std::identity id;
>     int i = 42;
>     assert(id(i) == 42);
>     assert(id(std::move(i)) == 42);
> 
>     MoveOnly m1 = 2;
>     MoveOnly m2 = id(std::move(m1));
>     assert(m2.value == 2);
> 
>     assert(&id(i) == &i);
>     assert(&id(std::move(i)) == &i);
>     assert(&id(std::move(m1) == &m1);
>     static_assert(&id(id) == &id);
> 
>     const std::identity idc;
>     assert(idc(1) == 1);
>     assert(std::move(id)(1) == 1);
>     assert(std::move(idc)(1) == 1);
> 
>     id = idc;  // test assignability
> 
>     static_assert(std::is_same_v<decltype(id(i)), int&>);
>     static_assert(std::is_same_v<decltype(id(std::declval<int&&>())), int&&>);
>     static_assert(std::is_same_v<decltype(id(std::declval<const int&>)), const int&>);
>     static_assert(std::is_same_v<decltype(id(std::declval<const int&&>)), const int&&>);
>     static_assert(std::is_same_v<decltype(id(std::declval<volatile int&>)), volatile int&>);
>     static_assert(std::is_same_v<decltype(id(std::declval<volatile int&&>)), volatile int&&>);
> 
>     return true;
> }
> 
> int main(int, char**) {
>     test();
>     static_assert(test());
> 
>     return 0;
> }
> ```
> 
> I don't think the test suite should depend on C++20 nor concepts. I don't think we need any more lines of test code than what I've written here.
...well, and also a test that `std::identity::is_transparent` is a typename.

    static_assert(std::is_same_v<std::void_t<std::identity::is_transparent>, void>);

should do the trick.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98151/new/

https://reviews.llvm.org/D98151



More information about the libcxx-commits mailing list