1

I'm been studying CERT C++ guidelines and I have a question related to ERR58-CPP. Handle all exceptions thrown before main() begins executing.

I believe avoiding to use static/global variables is good for tonnes of reasons, and it is not make sense to throw exception before main(). Even though, there are some cases that global variables which needs to be constructed before main() and wrapping such classes to make their constructor never throw is a good idea.

However, Scott Meyers said, one should never derive from std::string. ( From Why should one not derive from c++ std string class? ) That is possibly due to slicing problems.

Is it okay to deriving from std::string with no additional member variable/function and not overriding destructor?

Tas
  • 7,023
  • 3
  • 36
  • 51
Byoungchan Lee
  • 1,372
  • 13
  • 28
  • Why would you want to do this – M.M Jun 09 '17 at 02:34
  • @m.m. because std string's constructor could throw. – Yakk - Adam Nevraumont Jun 09 '17 at 02:36
  • You may replace std::string with any kind of third party classes with no virtual destructor. – Byoungchan Lee Jun 09 '17 at 02:38
  • You don't need any static allocations to happen before main. In fact, the common way to avoid the [Static Initialization Order Fiasco](https://isocpp.org/wiki/faq/ctors#static-init-order) is to delay construction of statics until used, which you can defer until after entering `main`. – paddy Jun 09 '17 at 02:41
  • 1
    Better roll your own `class MyString` which has a `std::string` data member and a conversion operator to `std::string`. This makes it behave like `std::string` and you can add methods and data member without having to worry about implementation details of `std::string`. – Henri Menke Jun 09 '17 at 02:49
  • What about using a helper function to create `std::string` ? Then you can handle exception there. – Hải Phạm Lê Jun 09 '17 at 06:13
  • If I return `std::string`, then copy assignment operator or move assignment operator may be called when I assign it to static storage. Copy assignment operator may throw, or none of assignment operator called by RVO. – Byoungchan Lee Jun 09 '17 at 06:32

0 Answers0