Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

= NULL, and non-static data member initializing in c++98

Tags:

c++

gcc

c++03

c++98

before all:
In C++98, C++03 - Non-static data member initialisers (NSDMIs) do not exist.
https://wandbox.org/ - online compiler you can change the gcc version etc.

Okay now let's consider some code (in c++98, or c++03):

#include <iostream>
struct test {
    void *x = NULL;
    void *y = 0;  //with (void*)0 here, we get the same results
};
int main() {
    std::cout<<(int)NULL;
}

Since gcc 4.8.1:

void *x = NULL;

is allowed (unexpected), but

void *y = 0;

is not (as expected). // getting "non-static data member initializers only available with -std=c++11 or -std=gnu++11" warning

The zero question is why 0 != NULL here (i thought that #define NULL 0,
or #define NULL (void *)0 )

Main question is why in newer gcc versions, we can initialize: void *x = NULL; without any warning - whereas this pointer is non static, and by default it is not set to NULL (by default void *x; is uninitialized).
And my additional question is how to force older gcc versions to accept it, or is there any tricks to make non static pointer members by default initialized to NULL.

im using: $ g++ prog.cc -Wall -Wextra -O2 -march=native -std=c++98 -pedantic-errors

like image 421
mariusz_latarnik01 Avatar asked Apr 02 '18 20:04

mariusz_latarnik01


People also ask

Can I initialize Non-static data member inside static block?

In the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.

Where do you initialize a non-static class member that is a reference?

Member initialization Non-static data members may be initialized in one of two ways: 1) In the member initializer list of the constructor.

How do you initialize a static member?

We can put static members (Functions or Variables) in C++ classesC++ classesA class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers private, protected or public.https://en.wikipedia.org › wiki › C++_classesC++ classes - Wikipedia. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

What is static and non-static member function in C++?

static members exist as members of the class rather than as an instance in each object of the class. There is only a single instance of each static data member for the entire class. Non-static member functions can access all data members of the class: static and non-static.


2 Answers

The zero question is why 0 != NULL here (i thought that #define NULL 0, or #define NULL (void *)0` )

GCC suppresses certain warnings about invalid code in system headers, such as using C++11 features in C++98 code. Because NULL is a macro defined in a system header GCC gets confused and thinks the invalid code is in a system header, so it doesn't warn here. When you use 0 it warns, because it isn't confused about the location.

N.B. (void*)0 is not a valid definition of NULL in C++, because it would mean this doesn't compile:

int* p = NULL;

In C you can convert void* to int* but not in C++.

Main question is why in newer gcc versions, we can initialize: void *x = NULL; without any warning - whereas this pointer is non static, and by default it is not set to NULL (by default void *x; is uninitialized).

It's a bug, GCC should give a diagnostic about this code.

And my additional question is how to force older gcc versions to accept it,

GCC 4.7 will accept it with a warning. You can't make older versions accept it (even with -std=c++0x), because support for default member initializers wasn't added until 4.7

or is there any tricks to make non static pointer members by default initialized to NULL.

Define a constructor and set the members there.

like image 74
Jonathan Wakely Avatar answered Sep 18 '22 01:09

Jonathan Wakely


Indeed, default member initializer for non-static data member is not allowed in C++98/C++03, and appears only in C++11.

struct test {
    void *x = NULL;
};

is accepted by GCC only due to a known bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103347 which is fixed in GCC 12.

like image 22
Fedor Avatar answered Sep 19 '22 01:09

Fedor