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

44 lines
1.1 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/PR23141
#include <functional>
#include <type_traits>
#include "test_macros.h"
struct Fun {
template<typename T, typename U>
TEST_CONSTEXPR_CXX20 void operator()(T &&, U &&) const {
static_assert(std::is_same<U, int &>::value, "");
}
};
TEST_CONSTEXPR_CXX20 bool test() {
std::bind(Fun{}, std::placeholders::_1, 42)("hello");
return true;
}
int main(int, char**) {
test();
#if TEST_STD_VER >= 20
static_assert(test());
#endif
return 0;
}