Is C++ context-free or context-sensitive?

Below is my (current) favorite demonstration of why parsing C++ is (probably) Turing-complete, since it shows a program which is syntactically correct if and only if a given integer is prime. So I assert that C++ is neither context-free nor context-sensitive. If you allow arbitrary symbol sequences on both sides of any production, you produce … Read more

#pragma once vs include guards? [duplicate]

I don’t think it will make a significant difference in compile time but #pragma once is very well supported across compilers but not actually part of the standard. The preprocessor may be a little faster with it as it is more simple to understand your exact intent. #pragma once is less prone to making mistakes … Read more

Why use pointers? [closed]

Why use pointers over normal variables? Short answer is: Don’t. 😉 Pointers are to be used where you can’t use anything else. It is either because the lack of appropriate functionality, missing data types or for pure perfomance. More below… When and where should I use pointers? Short answer here is: Where you cannot use … Read more

When to use references vs. pointers

Use reference wherever you can, pointers wherever you must. Avoid pointers until you can’t. The reason is that pointers make things harder to follow/read, less safe and far more dangerous manipulations than any other constructs. So the rule of thumb is to use pointers only if there is no other choice. For example, returning a … Read more

std::string to char*

It won’t automatically convert (thank god). You’ll have to use the method c_str() to get the C string version. std::string str = “string”; const char *cstr = str.c_str(); Note that it returns a const char *; you aren’t allowed to change the C-style string returned by c_str(). If you want to process it you’ll have … Read more