| 1 |
// ensure that dependent and non-dependent asserts inside templated |
| 2 |
// functions parse without error whether the function is instatiated or not |
| 3 |
// ensure that assert contract checks are generated inside called templates |
| 4 |
// ensure that template functions can be used as assert predicates |
| 5 |
// { dg-do run } |
| 6 |
// { dg-options "-std=c++2a -fcontracts -fcontract-continuation-mode=on" } |
| 7 |
|
| 8 |
template<typename T> |
| 9 |
int fun1(int a, T b) |
| 10 |
{ |
| 11 |
[[ assert: a > 0 ]]; |
| 12 |
[[ assert: (long long)b > 0 ]]; |
| 13 |
return a > 0; |
| 14 |
} |
| 15 |
|
| 16 |
template<typename T> |
| 17 |
struct test |
| 18 |
{ |
| 19 |
static int fun(int a, T b) { |
| 20 |
[[ assert: a > 0 ]]; |
| 21 |
[[ assert: b > 0 ]]; |
| 22 |
return a > 0; |
| 23 |
} |
| 24 |
}; |
| 25 |
|
| 26 |
int main() |
| 27 |
{ |
| 28 |
fun1(1, -1); |
| 29 |
fun1(-1, 1.0); |
| 30 |
fun1(-1, (const char *)0x1234); |
| 31 |
|
| 32 |
[[ assert: fun1(-1, -5) ]]; |
| 33 |
[[ assert: test<int>::fun(10, -6) ]]; |
| 34 |
[[ assert: test<double>::fun(10.0, -7) ]]; |
| 35 |
// return 0; |
| 36 |
} |
| 37 |
|
| 38 |
// { dg-skip-if "requires hosted libstdc++ for stdc++exp" { ! hostedlib } } |
| 39 |
// { dg-output {contract violation in function fun1<int> at .*:12: \(long long\)b > 0(\n|\r\n|\r)} } |
| 40 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 41 |
// { dg-output {contract violation in function fun1<double> at .*:11: a > 0(\n|\r\n|\r)} } |
| 42 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 43 |
// { dg-output {contract violation in function fun1<const char\*> at .*:11: a > 0(\n|\r\n|\r)} } |
| 44 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 45 |
// { dg-output {contract violation in function fun1<int> at .*:11: a > 0(\n|\r\n|\r)} } |
| 46 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 47 |
// { dg-output {contract violation in function fun1<int> at .*:12: \(long long\)b > 0(\n|\r\n|\r)} } |
| 48 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 49 |
// { dg-output {contract violation in function main at .*:32: fun1\(-1, -5\)(\n|\r\n|\r)} } |
| 50 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 51 |
// { dg-output {contract violation in function test<int>::fun at .*:21: b > 0(\n|\r\n|\r)} } |
| 52 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |
| 53 |
// { dg-output {contract violation in function test<double>::fun at .*:21: b > 0(\n|\r\n|\r)} } |
| 54 |
// { dg-output {\[continue:on\](\n|\r\n|\r)} } |