TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24866 19
A matter of style or is it?

Which one do you prefer (the constructor does some work and sets valid_):

class A {
public:
explicit operator bool() const { return valid; }

private:
bool valid_ = false;
}


or

class A {
public:
bool is_valid() const { return valid; }

private:
bool valid_ = false;
}


On the caller side:

A a{};
if (a) {
// is valid
}


or

A a{}
if (a.is_valid()) {
// is valid
}


https://redd.it/1rz8gq5
@r_cpp
Reddit From the cpp community on Reddit Explore this post and more from the cpp community
More from @r_cpp
  1. Oct 6, 2026C++26: Module improvements https://www.sandordargo.com/blog/2026/09/09/cpp26-modules https…
  2. Oct 6, 2026Tuning a Server for Benchmarking https://david.alvarezrosa.com/posts/tuning-a-server-for-b…
  3. Oct 6, 2026boost.org has been rebuilt: new profiles, badges, search, and library filtering The Boost…
  4. Oct 6, 2026When (Not) to Use alloca() https://voithos.io/articles/when-not-to-use-alloca/ https://red…
  5. Oct 6, 2026MSVC Build Tools Preview updates – September 2026 https://devblogs.microsoft.com/cppblog/m…
  6. Oct 6, 2026I built MrDocs, an open-source C++ reference doc generator on the Clang/LLVM AST. Looking…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →