Louis Dionne 13f5579cae [libc++] Make std::bind constexpr-friendly
std::bind is supposed to be constexpr-friendly since C++20 and it was
marked as such in our synopsis. However, the tests were not actually
testing any of it and as it happens, std::bind was not really constexpr
friendly. This fixes the issue and makes sure that at least some of the
tests are running in constexpr mode.

Some tests for std::bind check functions that return void, and those
use global variables. These tests haven't been made constexpr-friendly,
however the coverage added by this patch should be sufficient to get
decent confidence.

Differential Revision: https://reviews.llvm.org/D149295
2023-05-03 12:27:06 -04:00

56 lines
1.3 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <functional>
// template<CopyConstructible Fn, CopyConstructible... Types>
// unspecified bind(Fn, Types...); // constexpr since C++20
// template<Returnable R, CopyConstructible Fn, CopyConstructible... Types>
// unspecified bind(Fn, Types...); // constexpr since C++20
// https://llvm.org/PR16343
#include <functional>
#include <cassert>
#include "test_macros.h"
struct multiply {
template <typename T>
TEST_CONSTEXPR_CXX20 T operator()(T a, T b) {
return a * b;
}
};
struct plus_one {
template <typename T>
TEST_CONSTEXPR_CXX20 T operator()(T a) {
return a + 1;
}
};
TEST_CONSTEXPR_CXX20 bool test() {
using std::placeholders::_1;
auto g = std::bind(multiply(), 2, _1);
assert(g(5) == 10);
assert(std::bind(plus_one(), g)(5) == 11);
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 20
static_assert(test());
#endif
return 0;
}