VirtuosoChris / VirtuosoConsole

Single-File header only C++ Quake-style console library.
Other
23 stars 8 forks source link

Maybe T of makeTemp() should be static? #2

Closed CapitanLiteral closed 7 years ago

CapitanLiteral commented 7 years ago

I dont really know why, but if I don't put this var in static it gives me a runtime exeption when I use "sum" command or "sumFive" Run-Time Chech Failure #3 - The variable 'temp' is being used without being initialized but when I do it stops failing. Whatch it and tell me please, I'll be glad to learn why or why don't.

VirtuosoChris commented 7 years ago

Hi, it looks like you have some sort of run time error checking enabled. In practice the variable not being initialized doesn't matter at all since it gets overwritten by the parser, but for cleanliness' sake I committed a patch to make sure the variable is default initialized. Your fix works perfectly fine, but my preferred fix is T temp{};

instead of

static T temp;

An explanation of what's happening is in the commit log which I have pasted directly below. Please let me know if you have any more questions or input!

User CaptainLiteral reported runtime debug error check where makeTemp() asserted that T was not initialized; The declaration :

T temp;

default constructs a class variable, but leaves primitive types (in this case int) uninitialized. A solution proposed for this was to make T static, since static variables are default initialized according to the C++ standard.

However, C++11 brace initializer ({}, without equals sign) default initializes POD primitive data types and classes both; To me this is semantically closer to what we’re actually trying to do and C++11 should be fairly universal at this point so this is my preferred fix.