Does using const on function parameters have any effect? Why does it not affect the function signature?

const is pointless when the argument is passed by value since you will not be modifying the caller’s object. Wrong. It’s about self-documenting your code and your assumptions. If your code has many people working on it and your functions are non-trivial then you should mark const any and everything that you can. When writing … Read more

What’s the significance of a C function declaration in parentheses apparently forever calling itself?

The function name g_atomic_int_compare_and_exchange_full is in parentheses. What’s the significance of this? Putting a function name in brackets avoids any macro expansion in case there is a function like macro with the same name. That means, g_atomic_int_compare_and_exchange_full(…) will use the macro while (g_atomic_int_compare_and_exchange_full)(…) will use the function. Why is this used? You cannot assign a … Read more

Can a function prototype typedef be used in function definitions?

You cannot define a function using a typedef for a function type. It’s explicitly forbidden – refer to 6.9.1/2 and the associated footnote: The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition. The intent is … Read more

Maximum number of parameters in function declaration

Yes, there are limits imposed by the implementation. Your answer is given in the bold text in the following excerpt from the C++ Standard. 1. C++ Language Annex B – Implementation quantities Because computers are finite, C + + implementations are inevitably limited in the size of the programs they can successfully process. Every implementation … Read more

Function default argument value depending on argument name in C++ [duplicate]

According to the C++17 standard (11.3.6 Default arguments) 9 A default argument is evaluated each time the function is called with no argument for the corresponding parameter. A parameter shall not appear as a potentially-evaluated expression in a default argument. Parameters of a function declared before a default argument are in scope and can hide … Read more

Whyever **not** declare a function to be `constexpr`?

Functions can only be declared constexpr if they obey the rules for constexpr — no dynamic casts, no memory allocation, no calls to non-constexpr functions, etc. Declaring a function in the standard library as constexpr requires that ALL implementations obey those rules. Firstly, this requires checking for each function that it can be implemented as … Read more

Function declaration in CoffeeScript

CoffeeScript uses function declarations (aka “named functions”) in just one place: class definitions. For instance, class Foo compiles to var Foo; Foo = (function() { function Foo() {} return Foo; })(); The reason CoffeeScript doesn’t use function declarations elsewhere, according to the FAQ: Blame Microsoft for this one. Originally every function that could have a … Read more

Why can’t I define a function inside another function?

It is not obvious why one is not allowed; nested functions were proposed a long time ago in N0295 which says: We discuss the introduction of nested functions into C++. Nested functions are well understood and their introduction requires little effort from either compiler vendors, programmers, or the committee. Nested functions offer significant advantages, […] … Read more

Alternative (K&R) C syntax for function declaration versus prototypes

The question you are asking is really two questions, not one. Most replies so far tried to cover the entire thing with a generic blanket “this is K&R style” answer, while in fact only a small part of it has anything to do with what is known as K&R style (unless you see the entire … Read more