Proper way to receive a lambda as parameter by reference

You cannot have an auto parameter. You basically have two options: Option #1: Use std::function as you have shown. Option #2: Use a template parameter: template<typename F> void f(F && lambda) { /* … */} Option #2 may, in some cases, be more efficient, as it can avoid a potential heap allocation for the embedded … Read more

Extracting C / C++ function prototypes

I use ctags # p = function declaration, f = function definition ctags -x –c-kinds=fp /usr/include/hal/libhal.h Also works with C++ ctags -x –c++-kinds=pf –language-force=c++ /usr/include/c++/4.4.1/bits/deque.tcc Note, you may need to add include paths, do this using the -I /path/to/includes.

Mongoose/MongoDB result fields appear undefined in Javascript

Solution You can call the toObject method in order to access the fields. For example: var itemObject = item.toObject(); console.log(itemObject.title); // “foo” Why As you point out that the real fields are stored in the _doc field of the document. But why console.log(item) => { title: “foo”, content: “bar” }? From the source code of … Read more

Declare main prototype

C language standard, draft n1256: 5.1.2.2.1 Program startup 1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* … */ } or with two parameters (referred to here as … Read more

Javascript when to use prototypes

Prototypes are an optimisation. A great example of using them well is the jQuery library. Every time you obtain a jQuery object by using $(‘.someClass’), that object has dozens of “methods”. The library could achieve that by returning an object: return { show: function() { … }, hide: function() { … }, css: function() { … Read more

How to set the prototype of a JavaScript object that has already been instantiated?

Update: ES6 now specifies Object.setPrototypeOf(object, prototype) . EDIT Feb. 2012: the answer below is no longer accurate. proto is being added to ECMAScript 6 as “normative optional” which means it isn’t required to be implemented but if it is, it must follow the given set of rules. This is currently unresolved but at least it … Read more

Why does a function with no parameters (compared to the actual function definition) compile?

All the other answers are correct, but just for completion A function is declared in the following manner: return-type function-name(parameter-list,…) { body… } return-type is the variable type that the function returns. This can not be an array type or a function type. If not given, then int is assumed. function-name is the name of … Read more

What are the valid signatures for C’s main() function?

The C11 standard explicitly mentions these two: int main(void); int main(int argc, char* argv[]); although it does mention the phrase “or equivalent” with the following footnote: Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on. In addition, … Read more

tech