Is pointer type a literal type?

  • A+
Category:Languages

A scalar type is defined as

Trait class that identifies whether T is a scalar type. A scalar type is a type that has built-in functionality for the addition operator without overloads (arithmetic, pointer, member pointer, enum and std::nullptr_t).

It inherits from integral_constant as being either true_type or false_type, depending on whether T is a scalar type, no matter its const and/or volative qualification.

It means pointer is scalar type.

Now if we go to definition of literal type:

A type is a literal type if it is:

  • a scalar type; or
  • a reference type; or
  • an array of literal type; or -a class type (Clause 9) that has all of the following properties:
    • it has a trivial destructor,
    • every constructor call and full-expression in the brace-or-equal-initializers for non-static data members (if any) is a constant expression (5.19),
    • it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and
    • all of its non-static data members and base classes are of literal types.

Now, combining above 2 statements, it means pointer is literal type. However pointer can not be constexpr. can someone please clarify?

further see following code:

int a = 7; constexpr int *pointer1 = &a; int main () {   int b = 4;   constexpr int *pointer2 = &b; } 

pointer1 is fine but pointer 2 gives error. does that mean pointer to global is fine but to automatic variable is not? Does standard mention this anywhere ?


Pointers are literal types. They can be constexpr under certain conditions:

[expr.const] 6

... [a pointer is constexpr if] it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the address of a function, or a null pointer value.

(Where "object with static storage duration" means a global or static object, or a subobject of such object.)

A demo:

int x;  int main() {     constexpr int *ptr = &x; // Compiles.      // Doesn't compile: `error: '& foo' is not a constant expression`     // int foo;     // constexpr int *bar = &foo; } 

Apparently GCC (with -pedantic-errors -std=c++11/14/17) happily accepts out-of-range constexpr pointer arithmetic: constexpr int *ptr = &x - 10;, which seems like a bug to me.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: