Thank you for your answer! Given your explanation, it is now clear to me that :caption: produces a
caption and a number associated to the listing, while :name: allows to
crossreference the listing.
However, I think that the documentation is IMHO misleading, because it does not make clear that both :name: and :caption: need to be used. Also the description of option :name: [1] is misleading, when it states "Define implicit target name that can be referenced by using ref.", because both :name: and :caption: need to be defined (although in the example there, both are defined).
To be more clear: given the example below, I expected at least two cross references being generated (bar and baz), but only the third (baz) is generated, although also the second has :caption: defined. However, correctly sphinx creates only one.
***************
See :numref:`foo`, :numref:`bar`, and :numref:`baz`.