Click here to Skip to main content
15,558,870 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi! Might be already described here but did not find :-(
Lets say, I have defined class with two operators (besides other f-nalty):
class MyClass
{
      public:
           operator bool() const;
           operator const std::string() const;
}

Now, whan I use this class the way a'la:
MyClass mc;
if(mc) // the bool operator invoked
{
      std::string myString{ mc };
}

If I expect that string constructed using copy operator and const std::string operator of MyClass but this is incorrect :-(

Instead of
operator bool() const;
used and string constructed using
basic_string( std::initializer_list<CharT> ilist,
              const Allocator& alloc = Allocator() );

Same happens if I write:
std::string a{true, true, true, false, true, 'a'};
or std::string b{true};

I see that this is initializer list but isn't it limited by char type elements? Does that mean that any value that can be converted to char value is allowed here?

What I have tried:

I've played with it using Compiler explorer and that's seems correct!?
Posted
Updated 26-Nov-22 16:45pm
Comments
0x01AA 26-Nov-22 11:00am    
And the code for operator bool() const; and also operator const std::string() const; loos like?
Richard MacCutchan 26-Nov-22 11:43am    
You have not explained exactly what happens when you run this code.

And the code for operator bool() const; and also operator const std::string() const; loos like?


smth like this :


class test
{
    std::string data;

    public:

    test(const std::string& d) : data{d}
    {}

    operator bool() const 
    {
        return !data.empty();
    }

    operator const std::string&() const
    {
        return data;
    }

};

-------------------------
You have not explained exactly what happens when you run this code.


.....
test t{"hello"};

....

if (t)
{
  std::string s{t};
  sdt::cout << s << "\n";
}

When I run it in console it printed out "face" (ascii for 0x01)
 
Share this answer
 
Comments
Jaan Laasalu 2021 27-Nov-22 5:58am    
> as you seen, {} is different from () sometimes
Hi! Once more - thnx, but i concern more about why string constructed not using copy constructor (using std::string& operator) but using bool operator of test class?
In conclusion - need to cover all by unit tests..
longjmp 28-Nov-22 2:41am    
I think you found the key.
======================================
Using and string construction, use
basic_string( std::initializer_list<chart> ilist,
const Allocator& alloc = Allocator() ).
=====================
std::initializer_list<chart> can accept {true, true, true, false, true, 'a'}.
Since sizeof(CharT) >= sizeof(bool), it will result in an "implicit conversion" from bool to CharT.

And this code compiles fine without errors or warnings.
USHORT u16 = 8.
std::initializer_list<uint32> list32 = {u16, u16};
change
MyClass mc;
if(mc) // the bool operator invoked
{
      std::string myString{ mc };
}

to:
MyClass mc;
	if (mc) // the bool operator invoked
	{
		std::string myString(mc);
	}


will call your
MyClass::operator const std::string();

as you seen, {} is different from () sometimes
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900