Path: blob/master/test/hotspot/gtest/metaprogramming/test_enableIf.cpp
41145 views
/*1* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allStatic.hpp"26#include "metaprogramming/enableIf.hpp"27#include "utilities/debug.hpp"28#include <type_traits>29#include "unittest.hpp"3031class EnableIfTest: AllStatic {32class A: AllStatic {33public:34template <bool condition>35static typename EnableIf<condition, char>::type test();36template <bool condition>37static typename EnableIf<!condition, long>::type test();38};3940static const bool A_test_true_is_char = sizeof(A::test<true>()) == sizeof(char);41STATIC_ASSERT(A_test_true_is_char);4243static const bool A_test_false_is_long = sizeof(A::test<false>()) == sizeof(long);44STATIC_ASSERT(A_test_false_is_long);45};4647template<typename T, ENABLE_IF(std::is_integral<T>::value)>48static T sub1(T x) { return x - 1; }4950TEST(TestEnableIf, one_decl_and_def) {51EXPECT_EQ(15, sub1(16));52}5354template<typename T, ENABLE_IF(std::is_integral<T>::value)>55static T sub2(T x);5657template<typename T, ENABLE_IF_SDEFN(std::is_integral<T>::value)>58T sub2(T x) { return x - 2; }5960TEST(TestEnableIf, separate_decl_and_def) {61EXPECT_EQ(14, sub2(16));62}6364template<typename T>65struct TestEnableIfNested {66template<typename U, ENABLE_IF(std::is_integral<U>::value)>67static U sub1(U x);68};6970template<typename T>71template<typename U, ENABLE_IF_SDEFN(std::is_integral<U>::value)>72U TestEnableIfNested<T>::sub1(U x) { return x - 1; }7374TEST(TestEnableIf, nested_separate_decl_and_def) {75EXPECT_EQ(15, TestEnableIfNested<void>::sub1(16));76}7778// Demonstrate workaround for non-dependent condition.79template<typename T>80struct TestEnableIfNonDependent {81// Dependent is used to make the ENABLE_IF condition dependent on82// the type parameters for this function.83template<typename Dependent = T, ENABLE_IF(std::is_same<int, Dependent>::value)>84static T value() { return T{}; }85static int instantiate() { return 5; }86};8788TEST(TestEnableIf, non_dependent) {89EXPECT_EQ(int{}, TestEnableIfNonDependent<int>::value());90// This fails to compile if the ENABLE_IF for value() directly uses91// T rather than indirectly via Dependent.92EXPECT_EQ(5, TestEnableIfNonDependent<void>::instantiate());93}949596