0

The following code appears in boost graph library manual available here.

enter image description here

I have no problem understanding what the code does. My questions are:

(1)Why is the graph object passed as a constant reference. const Graph& g ?

(2)Why is the edge descriptor not passed similarly, but simply by value?

As I understand, the answer to (1) is that it imposes a constraint to ensure that the function is_self_loop() does not modify the object g and also that no copy is made of g within the function.

On the other hand, a copy is indeed made within the function of the edge_descriptor.

Is there any reason to accord these two different reasonings for the two passed objects? The function does not seem to alter e either, so why not pass it as a const and also to prevent a local copy, why not also pass it as a reference? Does this have to do with the sizes of g and e? That is, copying e is not a costly operation? If yes, how can the user figure out which object is "easy" to copy and which object is expensive to copy?

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • https://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference – user202729 Nov 02 '17 at 05:56
  • The descriptors are _descriptors_: they are by definition efficient to copy. Compilers have a much easier time optimizing values than references (aliasing is a pain), see the comments at https://stackoverflow.com/questions/21605579/how-true-is-want-speed-pass-by-value/21607854 – sehe Nov 02 '17 at 08:08

0 Answers0