Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Is this code fragment OK or does it result in undefined behavior?
std::string s; assert(strlen(s.c_str())==0);
If it isn't undefined behavior, will the above assertion pass?
That is perfectly well defined and the assertion passes. The c_str() function will always return a valid zero terminated C string.
One would normally use empty() to test for an empty string.
Add a comment
Yes it will work (if you append () to c_str to make it actually call the function) and the assertion will pass.
()
c_str
It's a compile error (if you have assertions enabled), since a const char *(std::string::*)(), cannot be converted to const char * implicitly.
const char *(std::string::*)()
const char *
(Tongue only halfway in cheek.)
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.