typedef int (*fp)(int);
int surrogate(int);
struct Incomplete;
struct X {
X() = default; X(const X&) = default; X(bool b) __attribute__((enable_if(b, "chosen when 'b' is true")));
void f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
void f(int n) __attribute__((enable_if(n == 1, "chosen when 'n' is one")));
void g(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
void h(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero")));
static void s(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero")));
void conflict(int n) __attribute__((enable_if(n+n == 10, "chosen when 'n' is five"))); void conflict(int n) __attribute__((enable_if(n*2 == 10, "chosen when 'n' is five")));
void hidden_by_argument_conversion(Incomplete n, int m = 0) __attribute__((enable_if(m == 10, "chosen when 'm' is ten")));
Incomplete hidden_by_incomplete_return_value(int n = 0) __attribute__((enable_if(n == 10, "chosen when 'n' is ten")));
operator long() __attribute__((enable_if(true, "chosen on your platform")));
operator int() __attribute__((enable_if(false, "chosen on other platform")));
operator fp() __attribute__((enable_if(false, "never enabled"))) { return surrogate; } };
void X::f(int n) __attribute__((enable_if(n == 0, "chosen when 'n' is zero"))) {
}
void X::f(int n) __attribute__((enable_if(n == 2, "chosen when 'n' is two"))) {
}
X x1(true);
X x2(false);
__attribute__((deprecated)) constexpr int old() { return 0; } void deprec1(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero"))); void deprec2(int i) __attribute__((enable_if(old() == 0, "chosen when old() is zero")));
void overloaded(int);
void overloaded(long);
struct Int {
constexpr Int(int i) : i(i) { }
constexpr operator int() const { return i; }
int i;
};
void default_argument(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero"))); void default_argument_promotion(int n, int m = Int(0)) __attribute__((enable_if(m == 0, "chosen when 'm' is zero")));
struct Nothing { };
template<typename T> void typedep(T t) __attribute__((enable_if(t, ""))); template<int N> void valuedep() __attribute__((enable_if(N == 1, "")));
int not_constexpr();
template<int N> void valuedep() __attribute__((enable_if(N == not_constexpr(), "")));
template <typename T> void instantiationdep() __attribute__((enable_if(sizeof(sizeof(T)) != 0, "")));
void test() {
X x;
x.f(0);
x.f(1);
x.f(2); x.f(3);
x.g(0);
x.g(1);
x.h(0);
x.h(1, 2);
x.s(0);
x.s(1);
X::s(0);
X::s(1);
x.conflict(5);
x.hidden_by_argument_conversion(10); x.hidden_by_incomplete_return_value(10);
deprec2(0);
overloaded(x);
default_argument(0);
default_argument(1, 2);
default_argument_promotion(0);
default_argument_promotion(1, 2);
int i = x(1);
Nothing n;
typedep(0); typedep(1);
typedep(n); }
template <typename T> class C {
void f() __attribute__((enable_if(T::expr == 0, ""))) {}
void g() { f(); }
};
int fn3(bool b) __attribute__((enable_if(b, ""))); template <class T> void test3() {
fn3(sizeof(T) == 1); }
template <typename T>
struct Y {
T h(int n, int m = 0) __attribute__((enable_if(m == 0, "chosen when 'm' is zero"))); };
void test4() {
Y<int> y;
int t0 = y.h(0);
int t1 = y.h(1, 2); }
void h(int);
template <typename T> void outer() {
void local_function() __attribute__((enable_if(::h(T()), "")));
local_function(); };
namespace PR20988 {
struct Integer {
Integer(int);
};
int fn1(const Integer &) __attribute__((enable_if(true, "")));
template <class T> void test1() {
int &expr = T::expr();
fn1(expr);
}
int fn2(const Integer &) __attribute__((enable_if(false, ""))); template <class T> void test2() {
int &expr = T::expr();
fn2(expr); }
int fn3(bool b) __attribute__((enable_if(b, ""))); template <class T> void test3() {
fn3(sizeof(T) == 1); }
}
namespace FnPtrs {
int ovlFoo(int m) __attribute__((enable_if(m > 0, "")));
int ovlFoo(int m);
void test() {
int (*p)(int) = ovlFoo;
int (*p2)(int) = &ovlFoo;
int (*a)(int);
a = ovlFoo;
a = &ovlFoo;
}
int ovlBar(int) __attribute__((enable_if(true, "")));
int ovlBar(int m) __attribute__((enable_if(false, "")));
void test2() {
int (*p)(int) = ovlBar;
int (*p2)(int) = &ovlBar;
int (*a)(int);
a = ovlBar;
a = &ovlBar;
}
int ovlConflict(int m) __attribute__((enable_if(true, "")));
int ovlConflict(int m) __attribute__((enable_if(1, "")));
void test3() {
int (*p)(int) = ovlConflict; int (*p2)(int) = &ovlConflict; int (*a)(int);
a = ovlConflict; a = &ovlConflict; }
template <typename T>
T templated(T m) __attribute__((enable_if(true, ""))) { return T(); }
template <typename T>
T templated(T m) __attribute__((enable_if(false, ""))) { return T(); }
void test4() {
int (*p)(int) = templated<int>;
int (*p2)(int) = &templated<int>;
int (*a)(int);
a = templated<int>;
a = &templated<int>;
}
template <typename T>
T templatedBar(T m) __attribute__((enable_if(m > 0, ""))) { return T(); }
void test5() {
int (*p)(int) = templatedBar<int>; int (*p2)(int) = &templatedBar<int>; int (*a)(int);
a = templatedBar<int>; a = &templatedBar<int>; }
template <typename T>
T templatedConflict(T m) __attribute__((enable_if(false, ""))) { return T(); }
template <typename T>
T templatedConflict(T m) __attribute__((enable_if(true, ""))) { return T(); }
template <typename T>
T templatedConflict(T m) __attribute__((enable_if(1, ""))) { return T(); }
void test6() {
int (*p)(int) = templatedConflict<int>; int (*p0)(int) = &templatedConflict<int>; int (*a)(int);
a = templatedConflict<int>; a = &templatedConflict<int>; }
int ovlNoCandidate(int m) __attribute__((enable_if(false, "")));
int ovlNoCandidate(int m) __attribute__((enable_if(0, "")));
void test7() {
int (*p)(int) = ovlNoCandidate; int (*p2)(int) = &ovlNoCandidate; int (*a)(int);
a = ovlNoCandidate; a = &ovlNoCandidate; }
int noOvlNoCandidate(int m) __attribute__((enable_if(false, "")));
void test8() {
int (*p)(int) = noOvlNoCandidate; int (*p2)(int) = &noOvlNoCandidate; int (*a)(int);
a = noOvlNoCandidate; a = &noOvlNoCandidate; }
}
namespace casting {
using VoidFnTy = void (*)();
void foo(void *c) __attribute__((enable_if(0, "")));
void foo(int *c) __attribute__((enable_if(c, "")));
void foo(char *c) __attribute__((enable_if(1, "")));
void testIt() {
auto A = reinterpret_cast<VoidFnTy>(foo);
auto AAmp = reinterpret_cast<VoidFnTy>(&foo);
using VoidFooTy = void (*)(void *);
auto B = reinterpret_cast<VoidFooTy>(foo);
auto BAmp = reinterpret_cast<VoidFooTy>(&foo);
using IntFooTy = void (*)(int *);
auto C = reinterpret_cast<IntFooTy>(foo);
auto CAmp = reinterpret_cast<IntFooTy>(&foo);
using CharFooTy = void (*)(void *);
auto D = reinterpret_cast<CharFooTy>(foo);
auto DAmp = reinterpret_cast<CharFooTy>(&foo);
}
void testItCStyle() {
auto A = (VoidFnTy)foo;
auto AAmp = (VoidFnTy)&foo;
using VoidFooTy = void (*)(void *);
auto B = (VoidFooTy)foo;
auto BAmp = (VoidFooTy)&foo;
using IntFooTy = void (*)(int *);
auto C = (IntFooTy)foo;
auto CAmp = (IntFooTy)&foo;
using CharFooTy = void (*)(void *);
auto D = (CharFooTy)foo;
auto DAmp = (CharFooTy)&foo;
}
}
namespace casting_templates {
template <typename T> void foo(T) {}
void foo(int *c) __attribute__((enable_if(c, ""))); void foo(char *c) __attribute__((enable_if(c, "")));
void testIt() {
using IntFooTy = void (*)(int *);
auto A = reinterpret_cast<IntFooTy>(foo); auto ARef = reinterpret_cast<IntFooTy>(&foo); auto AExplicit = reinterpret_cast<IntFooTy>(foo<int*>);
using CharFooTy = void (*)(char *);
auto B = reinterpret_cast<CharFooTy>(foo); auto BRef = reinterpret_cast<CharFooTy>(&foo); auto BExplicit = reinterpret_cast<CharFooTy>(foo<char*>);
}
void testItCStyle() {
using IntFooTy = void (*)(int *);
constexpr auto A = (IntFooTy)foo;
constexpr auto ARef = (IntFooTy)&foo;
constexpr auto AExplicit = (IntFooTy)foo<int*>;
using CharFooTy = void (*)(char *);
constexpr auto B = (CharFooTy)foo;
constexpr auto BRef = (CharFooTy)&foo;
constexpr auto BExplicit = (CharFooTy)foo<char*>;
static_assert(A == ARef && ARef == AExplicit, "");
static_assert(B == BRef && BRef == BExplicit, "");
}
}
namespace multiple_matches {
using NoMatchTy = void (*)();
void foo(float *c); void foo(int *c) __attribute__((enable_if(1, ""))); void foo(char *c) __attribute__((enable_if(1, "")));
void testIt() {
auto A = reinterpret_cast<NoMatchTy>(foo); auto ARef = reinterpret_cast<NoMatchTy>(&foo);
auto C = (NoMatchTy)foo; auto CRef = (NoMatchTy)&foo; }
}
namespace PR27122 {
namespace ns {
void Function(int num)
__attribute__((enable_if(num != 0, "")));
void Function(int num, int a0)
__attribute__((enable_if(num != 1, "")));
}
using ns::Function; void Run() {
Functioon(0); Functioon(0, 1); Functioon(0, 1, 2); }
void regularEnableIf(int a) __attribute__((enable_if(a, ""))); void runRegularEnableIf() {
regularEnableIf(0, 2); regularEnableIf(1, 2); regularEnableIf();
::PR27122::regularEnableIf(0, 2); ::PR27122::regularEnableIf(1, 2); ::PR27122::regularEnableIf(); }
struct Foo {
void bar(int i) __attribute__((enable_if(i, ""))); };
void runFoo() {
Foo f;
f.bar(); f.bar(1, 2); }
}
namespace dependent {
int error(int N) __attribute__((enable_if(N, ""))); int error(int N) __attribute__((enable_if(!N, ""))); template <int N> int callUnavailable() {
return error(N); }
constexpr int noError(int N) __attribute__((enable_if(N, ""))) { return -1; }
constexpr int noError(int N) __attribute__((enable_if(!N, ""))) { return -1; }
constexpr int noError(int N) { return 0; }
template <int N>
constexpr int callNoError() { return noError(N); }
static_assert(callNoError<0>() == 0, "");
static_assert(callNoError<1>() == 0, "");
template <int N> constexpr int templated() __attribute__((enable_if(N, ""))) {
return 1;
}
constexpr int A = templated<0>(); static_assert(templated<1>() == 1, "");
template <int N> constexpr int callTemplated() { return templated<N>(); }
constexpr int B = 10 + callTemplated<0>(); static_assert(callTemplated<1>() == 1, "");
}
namespace variadic {
void foo(int a, int b = 0, ...) __attribute__((enable_if(a && b, "")));
void testFoo() {
foo(1, 1);
foo(1, 1, 2);
foo(1, 1, 2, 3);
foo(1, 0); foo(1, 0, 2); foo(1, 0, 2, 3);
int m;
foo(1, 1);
foo(1, 1, m);
foo(1, 1, m, 3);
foo(1, 0); foo(1, 0, m); foo(1, 0, m, 3); }
}
namespace member_loc {
struct Foo { void bar() __attribute__((enable_if(0, ""))); }; void testFoo() {
Foo()
.bar(); }
}
namespace template_instantiation {
template <typename T>
struct Foo {
void bar(int a) __attribute__((enable_if(a, ""))); };
void runFoo() {
Foo<double>().bar(0); Foo<double>().bar(1);
}
}
namespace instantiate_constexpr_in_enable_if {
template<typename T> struct X {
static constexpr bool ok() { return true; }
void f() __attribute__((enable_if(ok(), "")));
};
void g() { X<int>().f(); }
}
namespace PR31934 {
int foo(int a) __attribute__((enable_if(a, "")));
int runFn(int (&)(int));
void run() {
{
int (&bar)(int) = foo; int baz = runFn(foo); }
{
int (&bar)(int) = (foo); int baz = runFn((foo)); }
{
int (&bar)(int) = static_cast<int (&)(int)>(foo); int baz = runFn(static_cast<int (&)(int)>(foo)); }
{
int (&bar)(int) = static_cast<int (&)(int)>((foo)); int baz = runFn(static_cast<int (&)(int)>((foo))); }
}
}
namespace TypeOfFn {
template <typename T, typename U>
struct is_same;
template <typename T> struct is_same<T, T> {
enum { value = 1 };
};
void foo(int a) __attribute__((enable_if(a, "")));
void foo(float a) __attribute__((enable_if(1, "")));
static_assert(is_same<__typeof__(foo)*, decltype(&foo)>::value, "");
}
namespace InConstantContext {
void foo(const char *s) __attribute__((enable_if(((void)__builtin_constant_p(*s), true), "trap"))) {}
void test() {
InConstantContext::foo("abc");
}
}
namespace StringLiteralDetector {
void need_string_literal(const char *p) __attribute__((enable_if(__builtin_constant_p(p), "argument is not a string literal"))); void test(const char *unknown) {
need_string_literal("foo");
need_string_literal(unknown); constexpr char str[] = "bar";
need_string_literal(str); }
}
namespace IgnoreUnusedArgSideEffects {
struct A { ~A(); };
void f(A a, bool b) __attribute__((enable_if(b, ""))); void test() {
f(A(), true);
f(A(), false); int n;
f((n = 1, A()), true);
f(A(), (n = 1, true)); f(A(), (A(), true));
}
#if __cplusplus > 201702L
struct B { constexpr ~B() {} bool b; };
void g(B b) __attribute__((enable_if(b.b, ""))); void test2() {
g(B{true});
g(B{false}); f(A(), B{true}.b);
f(A(), B{false}.b); }
int &h() __attribute__((enable_if((A(), true), "")));
float &h() __attribute__((enable_if((B(), true), "")));
float &x = h();
#endif
}
namespace DefaultArgs {
void f(int n = __builtin_LINE()) __attribute__((enable_if(n == 12345, "only callable on line 12345"))); void g() { f(); } #line 12345
void h() { f(); }
template<typename T> void x(int n = T()) __attribute__((enable_if(n == 0, ""))) {} void y() { x<int>(); }
struct Z { constexpr operator int() const { return 1; } };
void z() { x<Z>(); } }