Thread: Implicit object conversion

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    66

    Implicit object conversion

    Hi folks,

    I've just figured out something interesting by accident. For demonstration purposes, let's assume we have two classes:
    Code:
    class one {
    public:
        explicit operator bool() const noexcept
        {
            std::cout << "one::operator bool" << std::endl;
            return {};
        }
        
        operator std::size_t() const noexcept
        {
            std::cout << "one::operator std::size_t" << std::endl;
            return {};
        }
    };
    
    
    class two {
    public:
        explicit operator bool() const noexcept
        {
            std::cout << "two::operator bool" << std::endl;
            return {};
        }
        
        operator std::size_t() noexcept // note the missing const
        {
            std::cout << "two::operator std::size_t" << std::endl;
            return {};
        }
    };
    The classes are mostly identical, though the conversion operator of the second class for `std::size_t' is non-const (contrary to the one in the first class).
    Code:
    int main(){
        one first;
        
        if (first) {
        }
        
        std::size_t i=first;
        
        std::cout << std::endl;
        
        two second;
        
        if (second) {
        }
        
        std::size_t j=second;
    }
    Running the program above prints this in the terminal:
    Code:
    one::operator bool
    one::operator std::size_t
    
    
    two::operator std::size_t
    two::operator std::size_t
    As you can see, making a conversion operator non-const changes the result of certain expressions in a boolean context.

    So my questions are:
    - Why does the compiler prefer to call operator `std::size_t' and then convert it to a boolean value in the second class?
    - What has the use of `const' to do with any of this?
    - What would a class with behaviour equal to class `one' but with a non-const operator `std::size_t' look like?

    Thanks!

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Why does the compiler prefer to call operator `std::size_t' and then convert it to a boolean value in the second class?
    O_o

    The, what would be required, cast to add `const` to `this` for the `bool` operator makes the `std::stize_t` conversion a better fit.

    What has the use of `const' to do with any of this?
    The compiler has to choose the better fit, according the standard rules, whenever overload resolution comes into places.

    What would a class with behaviour equal to class `one' but with a non-const operator `std::size_t' look like?
    If you consider where each operator would be used, you can't write any such class without simply providing both forms of both operators.

    Soma

    Code:
    #include <iostream>
    
    struct SExample
    {
         void doSomething() const
         {
              std::cout << "const" << '\n';
         }
         void doSomething()
         {
              std::cout << "non-const" << '\n';
         }
    };
    
    void DoSomething
    (
         const SExample &
    )
    {
         std::cout << "const" << '\n';
    }
    
    void DoSomething
    (
         SExample &
    )
    {
         std::cout << "non-const" << '\n';
    }
    
    int main()
    {
         SExample s1;
         const SExample s2;
         s1.doSomething();
         s2.doSomething();
         DoSomething(s1);
         DoSomething(s2);
         return(0);
    }
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    66
    Well explained, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::shared_ptr<T> implicit conversion from T*
    By Elkvis in forum C++ Programming
    Replies: 25
    Last Post: 05-21-2013, 04:22 PM
  2. Implicit conversion between pointers?
    By nonlinearly in forum C Programming
    Replies: 13
    Last Post: 11-13-2011, 11:02 AM
  3. Implicit conversion problem
    By Elysia in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2008, 01:38 PM
  4. implicit conversion not working
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2004, 10:50 AM
  5. implicit conversion
    By cj56 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2003, 11:36 AM

Tags for this Thread