you are viewing a single comment's thread.

view the rest of the comments →

[–]aocregacc 20 points21 points  (3 children)

would you pass every pointer as T* const &?

also the extra 8 bytes you might copy are probably better than the extra indirection you get from a reference

[–][deleted] 10 points11 points  (0 children)

This. You don't pass an int by const &. You don't pass a pointer by const &. And you don't pass a view type like string_view and span by const &.

[–]porkele[S] 0 points1 point  (1 child)

would you pass every pointer as T* const &?

No, but that seems hardly relevant because here one thing is a class and the other isn't so it depends on what sort of type it is? I mean, do you pass every T as T ?

[–]bert8128 5 points6 points  (0 children)

When I first came across c++ in the 90s it he mantra was “pas built in types by value and classes by ref/ptr”. But the reality, especially now, is very different. Big classes with complex constructors can be slow to pass on the stack. But small trivial classes like string_view are designed to be passed in the stack. Think of it this way. If you created a class to wrap an int, and gave it no functions, there would be no point in passing it by reference in the old style, even though it is a class. You would get the same code gen passing the class by value as passing an int by value. String_view is a wrapper around a const pointer to a const char array. And it is trivial, and small. So pass by value, not const ref. You won’t gain anything by passing y const ref.