Search code examples
c++stringraii

C++ std::string is 100% RAII on any platform?


As I know, std::string is a RAII object so I don't need to initialize after declaration. The constructor will take care of this stuff automatically. But is there any exception on any platform or any compiler ?

class TestString
{
public:
    TestString()
    {
        // m_str.clear(); or
        // m_str = "";
    }
    virtual ~TestString(){}

private:
    std::string m_str;
};

Very simple question, thanks !


Solution

  • The default constructor for std::string constructs an empty string, with a length of zero characters. That is the guarantee offered by the default constructor on all platforms for all implementations.