Why does the local variable of an enhanced for loop have to be local? [duplicate]

Look at how the for-each loop internally works, see How does the Java ‘for each’ loop work? for(Iterator<String> i = someList.iterator(); i.hasNext(); ) { String item = i.next(); System.out.println(item); } Each time it declares the String variable item. Hence in your case its doing essentially State state; \\and inside State state = i.next(); which obviously … Read more

Why do the older C language specs require function-local variables to be declared up-front?

Looking at the early (6th edition Unix, 1975) C manual from Dennis Ritchie’s home page, in that version function-local variables could only be declared at the beginning of a function: The function-statement is just a compound statement which may have declarations at the start. function-statement: { declaration-listopt statement-list } declaration-list is not defined (an omission), … Read more

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

These are pretty tricky constructs, and it’s tough to give clear explanations; they aren’t as straightforward as Lisp macros (or, for that matter, the relationship between Lisp’s QUOTE and EVAL). However, there’s a good, lengthy discussion available in the form of notes from Robby Villegas’s 1999 talk “Unevaluated Expressions” on Wolfram’s website. Defer is omitted … Read more

Why does a collection initializer expression require IEnumerable to be implemented?

An object initializer doesn’t; a collection initializer does. It’s so that it’s applied to classes which really represent collections, rather than just arbitrary ones which have an Add method. I have to admit that every so often I’ve “implemented” IEnumerable explicitly, just to allow collection initializers – but thrown a NotImplementedException from GetEnumerator(). Note that … Read more

Is it possible to create a quine in every turing-complete language?

Any programming language which is Turing complete, and which is able to output any string (by a computable function of the string as program — this is a technical condition that is satisfied in every programming language in existence) has a quine program (and, in fact, infinitely many quine programs, and many similar curiosities) as … Read more

Why is sizeof(bool) not defined to be one by the C++ standard?

The other likely size for it is that of int, being the “efficient” integer type for the platform. On architectures where it makes any difference whether the implementation chooses 1 or sizeof(int) there could be a trade-off between size (but if you’re happy to waste 7 bits per bool, why shouldn’t you be happy to … Read more

Why do we need both using-directives and include-directives?

using directives and include preprocessor directives are two different things. The include roughly corresponds to the CLASSPATH environment variable of Java, or the -cp option of the java virtual machine. What it does is making the types known to the compiler. Just including <string> for example will make you able to refer to std::string : … Read more

Why can function templates not be partially specialized?

AFAIK that’s changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member of a class). You might look up the relevant DR (Defect Report), if there is one. EDIT: checking this, I find … Read more

Is it allowed to use unions for type punning, and if not, why?

To re-iterate, type-punning through unions is perfectly fine in C (but not in C++). In contrast, using pointer casts to do so violates C99 strict aliasing and is problematic because different types may have different alignment requirements and you could raise a SIGBUS if you do it wrong. With unions, this is never a problem. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)