Implement LWG2350: min, max, and minmax should be constexpr.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@201697 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2014-02-19 16:51:35 +00:00
parent a2df82b98e
commit 9d9463a355
13 changed files with 225 additions and 57 deletions

View File

@@ -43,4 +43,12 @@ int main()
test(x, y, x);
test(y, x, x);
}
#if _LIBCPP_STD_VER > 11
{
constexpr int x = 1;
constexpr int y = 0;
static_assert(std::max(x, y) == x, "" );
static_assert(std::max(y, x) == x, "" );
}
#endif
}

View File

@@ -45,4 +45,12 @@ int main()
test(x, y, std::greater<int>(), y);
test(y, x, std::greater<int>(), y);
}
#if _LIBCPP_STD_VER > 11
{
constexpr int x = 1;
constexpr int y = 0;
static_assert(std::max(x, y, std::greater<int>()) == y, "" );
static_assert(std::max(y, x, std::greater<int>()) == y, "" );
}
#endif
}

View File

@@ -31,5 +31,12 @@ int main()
assert(i == 3);
i = std::max({1, 3, 2});
assert(i == 3);
#if _LIBCPP_STD_VER > 11
{
static_assert(std::max({1, 3, 2}) == 3, "");
static_assert(std::max({2, 1, 3}) == 3, "");
static_assert(std::max({3, 2, 1}) == 3, "");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -32,5 +32,12 @@ int main()
assert(i == 1);
i = std::max({1, 3, 2}, std::greater<int>());
assert(i == 1);
#if _LIBCPP_STD_VER > 11
{
static_assert(std::max({1, 3, 2}, std::greater<int>()) == 1, "");
static_assert(std::max({2, 1, 3}, std::greater<int>()) == 1, "");
static_assert(std::max({3, 2, 1}, std::greater<int>()) == 1, "");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -43,4 +43,12 @@ int main()
test(x, y, y);
test(y, x, y);
}
#if _LIBCPP_STD_VER > 11
{
constexpr int x = 1;
constexpr int y = 0;
static_assert(std::min(x, y) == y, "" );
static_assert(std::min(y, x) == y, "" );
}
#endif
}

View File

@@ -45,4 +45,12 @@ int main()
test(x, y, std::greater<int>(), x);
test(y, x, std::greater<int>(), x);
}
#if _LIBCPP_STD_VER > 11
{
constexpr int x = 1;
constexpr int y = 0;
static_assert(std::min(x, y, std::greater<int>()) == x, "" );
static_assert(std::min(y, x, std::greater<int>()) == x, "" );
}
#endif
}

View File

@@ -31,5 +31,12 @@ int main()
assert(i == 1);
i = std::min({1, 3, 2});
assert(i == 1);
#if _LIBCPP_STD_VER > 11
{
static_assert(std::min({1, 3, 2}) == 1, "");
static_assert(std::min({2, 1, 3}) == 1, "");
static_assert(std::min({3, 2, 1}) == 1, "");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -32,5 +32,12 @@ int main()
assert(i == 3);
i = std::min({1, 3, 2}, std::greater<int>());
assert(i == 3);
#if _LIBCPP_STD_VER > 11
{
static_assert(std::min({1, 3, 2}, std::greater<int>()) == 3, "");
static_assert(std::min({2, 1, 3}, std::greater<int>()) == 3, "");
static_assert(std::min({3, 2, 1}, std::greater<int>()) == 3, "");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -45,4 +45,18 @@ int main()
test(x, y, y, x);
test(y, x, y, x);
}
#if _LIBCPP_STD_VER > 11
{
// Note that you can't take a reference to a local var, since
// it's address is not a compile-time constant.
constexpr static int x = 1;
constexpr static int y = 0;
constexpr auto p1 = std::minmax (x, y);
static_assert(p1.first == y, "");
static_assert(p1.second == x, "");
constexpr auto p2 = std::minmax (y, x);
static_assert(p2.first == y, "");
static_assert(p2.second == x, "");
}
#endif
}

View File

@@ -27,6 +27,7 @@ test(const T& a, const T& b, C c, const T& x, const T& y)
assert(&p.second == &y);
}
int main()
{
{
@@ -47,4 +48,18 @@ int main()
test(x, y, std::greater<int>(), x, y);
test(y, x, std::greater<int>(), x, y);
}
#if _LIBCPP_STD_VER > 11
{
// Note that you can't take a reference to a local var, since
// it's address is not a compile-time constant.
constexpr static int x = 1;
constexpr static int y = 0;
constexpr auto p1 = std::minmax(x, y, std::greater<>());
static_assert(p1.first == x, "");
static_assert(p1.second == y, "");
constexpr auto p2 = std::minmax(y, x, std::greater<>());
static_assert(p2.first == x, "");
static_assert(p2.second == y, "");
}
#endif
}

View File

@@ -25,5 +25,15 @@ int main()
assert((std::minmax({2, 3, 1}) == std::pair<int, int>(1, 3)));
assert((std::minmax({3, 1, 2}) == std::pair<int, int>(1, 3)));
assert((std::minmax({3, 2, 1}) == std::pair<int, int>(1, 3)));
#if _LIBCPP_STD_VER > 11
{
static_assert((std::minmax({1, 2, 3}) == std::pair<int, int>(1, 3)), "");
static_assert((std::minmax({1, 3, 2}) == std::pair<int, int>(1, 3)), "");
static_assert((std::minmax({2, 1, 3}) == std::pair<int, int>(1, 3)), "");
static_assert((std::minmax({2, 3, 1}) == std::pair<int, int>(1, 3)), "");
static_assert((std::minmax({3, 1, 2}) == std::pair<int, int>(1, 3)), "");
static_assert((std::minmax({3, 2, 1}) == std::pair<int, int>(1, 3)), "");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}

View File

@@ -26,5 +26,15 @@ int main()
assert((std::minmax({2, 3, 1}, std::greater<int>()) == std::pair<int, int>(3, 1)));
assert((std::minmax({3, 1, 2}, std::greater<int>()) == std::pair<int, int>(3, 1)));
assert((std::minmax({3, 2, 1}, std::greater<int>()) == std::pair<int, int>(3, 1)));
#if _LIBCPP_STD_VER > 11
{
static_assert((std::minmax({1, 2, 3}, std::greater<int>()) == std::pair<int, int>(3, 1)), "");
static_assert((std::minmax({1, 3, 2}, std::greater<int>()) == std::pair<int, int>(3, 1)), "");
static_assert((std::minmax({2, 1, 3}, std::greater<int>()) == std::pair<int, int>(3, 1)), "");
static_assert((std::minmax({2, 3, 1}, std::greater<int>()) == std::pair<int, int>(3, 1)), "");
static_assert((std::minmax({3, 1, 2}, std::greater<int>()) == std::pair<int, int>(3, 1)), "");
static_assert((std::minmax({3, 2, 1}, std::greater<int>()) == std::pair<int, int>(3, 1)), "");
}
#endif
#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
}