Always use type-appropriate mechanisms to represent the absence of a value. For objects, use default constructors rather than nullptr. For potentially absent values, consider using optional types. For pointers, use null checks before performing operations on them.
Always use type-appropriate mechanisms to represent the absence of a value. For objects, use default constructors rather than nullptr. For potentially absent values, consider using optional types. For pointers, use null checks before performing operations on them.
Examples:
bias_md = nullptr;
when bias_md
is an object type (dnnl::memory::desc)CORRECT: bias_md = dnnl::memory::desc();
to represent an “empty” descriptor
ncclHeartbeatMonitorThread_ = std::thread(...);
without checking current state// Check nullness before assignment to prevent issues on second call
if (!ncclHeartbeatMonitorThread_.joinable()) {
ncclHeartbeatMonitorThread_ = std::thread(...);
}
This practice improves type safety, prevents null pointer dereferences, and creates more maintainable code by using language features designed for representing absence of values.
Enter the URL of a public GitHub repository