liveBook · Manning

Zero value

As explained in 100 C++ Mistakes and How to Avoid Them

A zero value is the type-dependent zero-initialized value assigned to an object or variable by a relevant initialization operation.

A zero value is the type-dependent value used to represent an uninitialized or invalid state. Do not assume every variable receives one: when initialization is missed, a value may be unpredictable, leading to undefined behavior and a broken class invariant. section 7.3

Listing 7.1 A typical textbook structure
struct Person {
    int age;
    std::string name;
};
 
int main() {
    Person p1;
    std::cout << p1.age << " '" << p1.name << "'\n";        #1
    Person p2 = Person();
    std::cout << p2.age << " '" << p2.name << "'\n";        #2
}

Compare the two ways of creating a Person and see how the age member’s initialization differs. section 7.3

For exclusive resources, set the source to a clear, detectable value after transfer. Use nullptr for pointers in modern C++; for value members, choose a zero value only if it does not violate the class invariant or represent a legitimate value. section 8.6

Listing 8.11 Care is taken for dynamic resources but not for unique resources
int createSocket() {
    // assume valid implementation; this is for an example only!
    static int sock_num = 0;
    return ++sock_num;
}
 
class Message {
private:
    std::string payload;
public:
    Message(const std::string& msg) : payload(msg) {}
    const std::string& getMessage() const { return payload; }
};
 
class Channel {
private:
    std::queue<Message*> messages;
    int socket;
public:
    Channel(int sock) : socket(sock) {}
    void setMessage(std::string msg) { messages.push(new Message(msg)); }
    int getSocket() const { return socket; }
    std::string getMessage() {
        std::string msg = messages.front()->getMessage();
        messages.pop();
        return msg;
    }
    Channel& operator=(Channel& o) {
        for (int i = 0; i < messages.size(); ++i)
            messages.pop();
        for (int i = 0; i < o.messages.size(); ++i) {
            messages.push(o.messages.front());
            o.messages.pop();
        }
        socket = o.socket;                                #1
        return *this;
    }
};
 
int main() {
    Channel one(createSocket());
    Channel two(createSocket());
    two = one;
    std::cout << one.getSocket() << ' ' << two.getSocket() << '\n';
}

Inspect the Channel assignment that handles queued messages but not its unique socket resource. section 8.6

Listing 8.12 Transferring dynamic and unique resources
int createSocket() {
    // assume valid implementation; this is for an example only!
    static int sock_num = 0;
    return ++sock_num;
}
 
class Message {
private:
    std::string payload;
public:
    Message(const std::string& msg) : payload(msg) {}
    const std::string& getMessage() const { return payload; }
};
 
class Channel {
private:
    std::queue<Message*> messages;
    int socket;
public:
    Channel(int sock) : socket(sock) {}
    void setMessage(std::string msg) { messages.push(new Message(msg)); }
    int getSocket() const { return socket; }
    std::string getMessage() {
        std::string msg = messages.front()->getMessage();
        messages.pop();
        return msg;
    }
    Channel& operator=(Channel& o) {
        for (int i = 0; i < o.messages.size(); ++i) {
            messages.push(new Message(o.messages.front()->getMessage()));
            o.messages.pop();
        }
        socket = createSocket();                              #1
        o.socket = 0;                                         #2
        return *this;
    }
};
 
int main() {
    Channel one(createSocket());
    Channel two(createSocket());
    two = one;
    std::cout << one.getSocket() << ' ' << two.getSocket() << '\n';

Compare the resource-transfer version, which also addresses the unique socket resource. section 8.6

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Defines the value through initialization and resource-transfer examples, and warns about invalid states without detailing language mechanics.
Examples
Uses a Person age member and Channel socket resources to illustrate initialization and post-transfer invalid values.
What you can do after
Apply a detectable invalid value after resource transfer, while preserving the class invariant.
How it connects
Connects zero values to class invariants, constructors, initialization, and resource ownership.

Also explained in

Compare how 5 books explain zero value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.