Talk:Knight's tour: Difference between revisions

→‎C++ formatting: Constness on local variables.
(→‎Incomplete Tours and Warnsdorff: Suggestions for improvement)
(→‎C++ formatting: Constness on local variables.)
Line 55:
 
I edited the C++ to remove some of the changes made. Several are fairly pointless (such as adding const to all the local variables; the compiler is smart enough to figure that out if it even makes a difference), and most were just reformatting it to someone else's formatting preferences (++i vs i++, etc). If there were a sitewide C++ formatting guide (a possibility suggested on the C++ discussion page, but not currently implemented) I (or whoever) can format it to fit that. [[User:MagiMaster|MagiMaster]] 03:43, 31 May 2011 (UTC)
: (Note: I didn't write the C++ code.) For trivial cases, const on local variables is unnecessary, but I'm not sure it was worth removing. Using const when a variable ''shouldn't'' change is often useful to avoid accidental mutation. (For similar reasons, I tend to write if statements as if(constval '''cmp''' nonconst)). Also, the compiler can't assume a local variable can be treated as const unless it can prove so, which goes out the window if a reference (or pointer-to) the variable is passed non-const to a function outside the current linkable object (the compiler can't know that the called function won't mutate the variable), and may also be very difficult under some local non-trivial operations involving its address or created references.
:
: A perfect compiler which took all logic optimizations to their every possible conclusion could be presumed to be able to treat non-const local variables as const (as long as pointers and references didn't go outside the current linkable object), but for any other compiler it's polite to drop hints.
:
: I do like the rest of the changes, though. --[[User:Short Circuit|Michael Mol]] 16:20, 31 May 2011 (UTC)