mardi 23 février 2021

C++11: inconsistent warnings for out-of-order instance member initialization

The following code crashes, raising std::bad_alloc or sometimes std::logic_error.

#include <string>

class Crasher {
public:
    Crasher() : value(default_value) {}
private:
    std::string value;
    const std::string default_value = "default is yours";
};


int main(void) {
    Crasher();
}

I believe the reason is that the members are initialized out of order, and when value is being intialized in initializer list, default_value hasn't yet been initialized.

However, I don't get any compiler warnings or errors with -Wall enabled. However if I change value and default_value to a POD type like int, then I get a warning from g++:

$ g++ -Wall crasher.cpp 
crasher2.cpp: In constructor ‘Crasher::Crasher()’:
crasher2.cpp:5:23: warning: ‘*<unknown>.Crasher::default_value’ is used uninitialized in this function [-Wuninitialized]
    5 |     Crasher() : value(default_value) {}
      |              

Why do I not get a warning when the type is std::string? Is this a compiler problem, or a quirk of the language?

Aucun commentaire:

Enregistrer un commentaire