Is the difference between two legal C ++ 17 pointers a constant expression?

In accordance with the cppreference section, Basic Constant Expressions point 19) the subtraction operator between two pointers is not a legal constant expression until C ++ 14 . Can I assume that the following code is legal C ++ 17 code, or is this interpretation an abuse?

int X, Y; template <long long V> struct S { }; int main() { S<&X - &Y> s; (void)s; } 
+6
source share
2 answers

A moot point. Arithmetic of a pointer is determined only by pointers belonging to the same array, which, of course, is not so. So, the code above is not legal C ++, and in fact it does not compile with the compilers available to me.

+14
source

The cppref article quoted says:

Kernel constant expression - any expression that does not have one of the following.

7) An expression whose evaluation leads to any form of the main language (since C ++ 17) undefined behavior (including integer overflow of integers, division by zero, pointer, arithmetic external array, bound , etc.). Regardless of whether the behavior of the standard library is undefined, undefined. (since C ++ 17)

19) the subtraction operator between two pointers (before C ++ 14)

Probably only the ptr arithemtics array inside the boundaries of the array gets "legalized" since C ++ 14, and not all arithmetic pointers


In fact, the demo shows that the ptr arithmetics array compiles well even with C ++ 11 (and not C ++ 98)

+3
source

Source: https://habr.com/ru/post/1013736/


All Articles