5

Consider the following code:

#include <iostream>

bool preInit()
{
    std::cerr << "Doing preinitialization...\n";
    return true;
}

const bool preinitialized=preInit();

int main()
{
    std::cerr << "In main()\nPreinitialization has "
              << (preinitialized ? "" : "not ") << "been done\n";
}

Is the use of std::cerr (and any other facility of the standard C++ library) safe in preInit() here? Is the library guaranteed to be ready for use before main() is called, according to the C++ standard?

Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • Are you asking for any particular standard or does the current one suffice? – NathanOliver Jun 30 '16 at 12:56
  • It'll just work (tm) – lorro Jun 30 '16 at 12:57
  • No problem at all. – Emerald Weapon Jun 30 '16 at 12:58
  • There is nothing different from executing code within main or in the global context. Just be careful because the order of initialisation of global objects is undefined. – Emerald Weapon Jun 30 '16 at 12:59
  • @EmeraldWeapon that's the potential problem: what if `std::cerr` depends on some global state, which needs static initialization? Can user objects' constructors appear to be called before the system ones? – Ruslan Jun 30 '16 at 13:18
  • @πάνταῥεῖ I don't think this is a duplicate. I've not found any discussion of safety of such action in the question linked. – Ruslan Jun 30 '16 at 13:19
  • @NathanOliver current would suffice, but if the answer is different for older standards, it'd be welcomed if the difference were highlighted. – Ruslan Jun 30 '16 at 13:22
  • @Ruslan To avoid initialisation fiasco you should never call a global variable within code executed before main. You need to wrap the global variable into an accessor function which statically contains that variable and initialises it internally on the first call. For example `inline Type* global_object() { static Type* obj= new Type(); return obj;}` – Emerald Weapon Jun 30 '16 at 13:31
  • 2
    I don't think this is a duplicate as marked. The other question merely asks "can I print something before main", without going into the legalities or safeness of doing so – Smeeheey Jun 30 '16 at 13:37
  • @Ruslan & Smeeheey to be fair, the distinction between "Can I do X in C++" and "Is it safe to do X in C++" is very subtle. Technically, you **can** have as much undefined behaviour in your program as you want, but normally, we programmers interpret "can" as "is it well defined and correct". What in particular are you concerned about beyond simply the correctness of the program? – eerorika Jun 30 '16 at 13:42
  • 1
    None of the answers in the suggested duplicate are satisfactory to your question in my opinion, but the comments by *David Rodríguez - dribeas* should be exactly what you are looking for. – eerorika Jun 30 '16 at 13:50
  • 1
    @NathanOliver nice one. That answers for the correctness concern in the case of the IO-objects well. That does leave us with a residue question: Are there other static objects in the standard library and do they have the same guarantee? (None spring to my mind). That's not easy to answer, because one would have to be very familiar with the entirety of the standard library to be able to answer it. – eerorika Jun 30 '16 at 14:04
  • @user2079303 None come to me but I wouldn't be surprised if there is. If we use the question in the question body then I think the dupe is okay. Doesn't fully cover the title but it covers the body. I'll change the dupe target and if the OP wants to make it more general we can reopen it again. – NathanOliver Jun 30 '16 at 14:06

0 Answers0