Skip to content

Commit

Permalink
GH-45099: [C++] Avoid static const variable in the status.h (#45100)
Browse files Browse the repository at this point in the history
### Rationale for this change

The `Status::message` function below has defined a static const string in the header file which may cause troubles in different translation units.

```
  const std::string& message() const {
    static const std::string no_message = "";
    return ok() ? no_message : state_->msg;
  }
```

### What changes are included in this PR?

Move the definition of `Status::message` function into the source file.

### Are these changes tested?

Pass CIs.

### Are there any user-facing changes?

No.
* GitHub Issue: #45099

Authored-by: Gang Wu <[email protected]>
Signed-off-by: Gang Wu <[email protected]>
  • Loading branch information
wgtmac authored Dec 25, 2024
1 parent e7471f8 commit a2f1988
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 10 additions & 0 deletions cpp/src/arrow/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ std::string Status::ToStringWithoutContextLines() const {
return message;
}

const std::string& Status::message() const {
static const std::string no_message = "";
return ok() ? no_message : state_->msg;
}

const std::shared_ptr<StatusDetail>& Status::detail() const {
static std::shared_ptr<StatusDetail> no_detail = NULLPTR;
return state_ ? state_->detail : no_detail;
}

void Status::Abort() const { Abort(std::string()); }

void Status::Abort(const std::string& message) const {
Expand Down
10 changes: 2 additions & 8 deletions cpp/src/arrow/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,10 @@ class ARROW_EXPORT [[nodiscard]] Status : public util::EqualityComparable<Status
constexpr StatusCode code() const { return ok() ? StatusCode::OK : state_->code; }

/// \brief Return the specific error message attached to this status.
const std::string& message() const {
static const std::string no_message = "";
return ok() ? no_message : state_->msg;
}
const std::string& message() const;

/// \brief Return the status detail attached to this message.
const std::shared_ptr<StatusDetail>& detail() const {
static std::shared_ptr<StatusDetail> no_detail = NULLPTR;
return state_ ? state_->detail : no_detail;
}
const std::shared_ptr<StatusDetail>& detail() const;

/// \brief Return a new Status copying the existing status, but
/// updating with the existing detail.
Expand Down

0 comments on commit a2f1988

Please sign in to comment.