CPP: Add a test involving templates.

This commit is contained in:
Geoffrey White
2019-09-25 19:18:00 +01:00
parent 4fc73cab63
commit 3f167a6f15
2 changed files with 35 additions and 0 deletions

View File

@@ -1,3 +1,6 @@
| template.cpp:4:7:4:15 | ... < ... | Check the comparison operator precedence. |
| template.cpp:4:7:4:15 | ... < ... | Check the comparison operator precedence. |
| template.cpp:10:7:10:15 | ... < ... | Check the comparison operator precedence. |
| test.cpp:42:6:42:14 | ... < ... | Check the comparison operator precedence. |
| test.cpp:43:6:43:14 | ... > ... | Check the comparison operator precedence. |
| test.cpp:44:6:44:16 | ... <= ... | Check the comparison operator precedence. |

View File

@@ -0,0 +1,32 @@
template <typename T>
void templateFunc1(T x, T y, T z) {
if (x < y < z) {} // BAD (though dubious as we can imagine other instantiations using an overloaded `operator<`)
if (x < y && y < z) {} // GOOD
};
template <typename T>
void templateFunc2(T x, T y, T z) {
if (x < y < z) {} // GOOD (used with an overloaded `operator<`) [FALSE POSITIVE]
if (x < y && y < z) {} // GOOD
};
struct myStruct {
operator bool() {
return true;
}
myStruct operator<(myStruct &other) {
return other; // non-standard `operator<` behaviour
}
};
int main() {
int x = 3;
myStruct y;
templateFunc1(x, x, x);
templateFunc2(y, y, y);
return 0;
}