| 1 |
// ensure that assert contract predicates that are not convertible to bool |
| 2 |
// generate an error |
| 3 |
// ensure the same for instatiated template functions |
| 4 |
// ensure the same for non-instatiated template functions when the predicate |
| 5 |
// is not dependent on the template parameters |
| 6 |
// ensure template parameter dependent, potentially non-boolean, contract |
| 7 |
// predicates do not generate an error if never instatiated |
| 8 |
// { dg-do compile } |
| 9 |
// { dg-options "-std=c++2a -fcontracts" } |
| 10 |
|
| 11 |
void fun() |
| 12 |
{ |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
template<typename T> |
| 17 |
void fun2(T a) |
| 18 |
{ |
| 19 |
[[assert: fun()]]; // { dg-error "could not convert|in argument" } |
| 20 |
} |
| 21 |
|
| 22 |
template<typename T> |
| 23 |
void fun3(T a) |
| 24 |
{ |
| 25 |
[[assert: fun()]]; // { dg-error "could not convert|in argument" } |
| 26 |
} |
| 27 |
|
| 28 |
template<typename T> |
| 29 |
void fun4(T a) |
| 30 |
{ |
| 31 |
[[assert: a.fun()]]; |
| 32 |
} |
| 33 |
|
| 34 |
struct test |
| 35 |
{ |
| 36 |
void fun() { } |
| 37 |
void fun2() { } |
| 38 |
}; |
| 39 |
|
| 40 |
template<typename T> |
| 41 |
void fun5(T a) |
| 42 |
{ |
| 43 |
[[assert: a.fun2()]]; // { dg-error "could not convert" } |
| 44 |
} |
| 45 |
|
| 46 |
struct VoidFun |
| 47 |
{ |
| 48 |
void fun() { } |
| 49 |
}; |
| 50 |
struct BoolFun |
| 51 |
{ |
| 52 |
bool fun() { return true; } |
| 53 |
}; |
| 54 |
|
| 55 |
template<typename T> |
| 56 |
void fun6(T a) |
| 57 |
{ |
| 58 |
[[ assert: a.fun() ]]; // { dg-error "could not convert" } |
| 59 |
} |
| 60 |
|
| 61 |
template void fun6(VoidFun); |
| 62 |
|
| 63 |
template<typename T> |
| 64 |
void fun7(T a) |
| 65 |
{ |
| 66 |
[[ assert: a.fun() ]]; |
| 67 |
} |
| 68 |
|
| 69 |
template void fun7(BoolFun); |
| 70 |
|
| 71 |
struct ImplicitBool |
| 72 |
{ |
| 73 |
operator bool() { return true; } |
| 74 |
}; |
| 75 |
struct ExplicitBool |
| 76 |
{ |
| 77 |
explicit operator bool() { return true; } |
| 78 |
}; |
| 79 |
|
| 80 |
template<typename T> |
| 81 |
void fun8(T a) |
| 82 |
{ |
| 83 |
[[ assert: T() ]]; |
| 84 |
} |
| 85 |
|
| 86 |
template void fun8(ImplicitBool); |
| 87 |
template void fun8(ExplicitBool); |
| 88 |
|
| 89 |
void fun9() |
| 90 |
{ |
| 91 |
[[ assert: ImplicitBool() ]]; |
| 92 |
[[ assert: ExplicitBool() ]]; |
| 93 |
} |
| 94 |
|
| 95 |
int main() |
| 96 |
{ |
| 97 |
[[assert: fun()]]; // { dg-error "could not convert" } |
| 98 |
fun2(1); |
| 99 |
|
| 100 |
test t; |
| 101 |
fun5(t); |
| 102 |
return 0; |
| 103 |
} |