Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:07:08+00:00 2026-06-18T14:07:08+00:00

classes in the stl, such as unique_ptr will occasionally show examples such as: //

  • 0

classes in the stl, such as unique_ptr will occasionally show examples such as:

// unique_ptr constructor example
#include <iostream>
#include <memory>

int main () {
  std::default_delete<int> d;
  std::unique_ptr<int> u1;
  std::unique_ptr<int> u2 (nullptr);
  std::unique_ptr<int> u3 (new int);
  std::unique_ptr<int> u4 (new int, d);
  std::unique_ptr<int> u5 (new int, std::default_delete<int>());
  std::unique_ptr<int> u6 (std::move(u5));
  std::unique_ptr<void> u7 (std::move(u6));
  std::unique_ptr<int> u8 (std::auto_ptr<int>(new int));

  std::cout << "u1: " << (u1?"not null":"null") << '\n';
  std::cout << "u2: " << (u2?"not null":"null") << '\n';
  std::cout << "u3: " << (u3?"not null":"null") << '\n';
  std::cout << "u4: " << (u4?"not null":"null") << '\n';
  std::cout << "u5: " << (u5?"not null":"null") << '\n';
  std::cout << "u6: " << (u6?"not null":"null") << '\n';
  std::cout << "u7: " << (u7?"not null":"null") << '\n';
  std::cout << "u8: " << (u8?"not null":"null") << '\n';

 *emphasized text* return 0;
}

The line:

 std::cout << "u1: " << (u1?"not null":"null") << '\n';

shows the unique_ptr u1 being directly cast to false if it is tracking a null pointer.

I have seen this behaviour used in other custom classes. How is this managed and what operator decides whether a direct cast to bool such as this returns true or false?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:07:10+00:00Added an answer on June 18, 2026 at 2:07 pm

    It is implemented as a member conversion operator of the form explicit operator bool() const;. Whether it returns true or false is implemented in the logic of the class itself. For example, this class has an bool conversion operator that returns true if it’s data member has value 42, and false otherwise:

    struct Foo
    {
      explicit operator bool() const { return n==42; }
      int n;
    };
    
    #include <iostream>
    
    int main()
    {
      Foo f0{12};
      Foo f1{42};
    
      std::cout << (f0 ? "true\n" : "false\n"); 
      std::cout << (f1 ? "true\n" : "false\n"); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem sorting my derived classes with the STL sort function. Example
I often have classes that are mostly just wrappers around some STL container, like
how can I create STL collection of classes which implement abstract base class using
It is well-known that STL classes do not use virtual methods anywhere (and STL
These three are shared pointer classes from Qt, STL and Boost, respectively. They seem
for example I have project with 200 classes (200 headers and 200 compilatioin units),
Why does the constructor and open method of the std::(i|o)fstream classes take the name
I have a bunch of classes which implement geometric objects based on vertices, such
There are operator classes in STL like less, equal_to, greater_equal etc. How to easily
What are the usual rules for Iterator invalidation when operating over STL container classes(Vector,Dequeue,list,map,multimap,set,multiset).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.