-1

Is the verbosity really worth it when passing read only vector in function?

const vector<const string>&

I usually have

const vector<string>&

which is already verbose enough. Now I see some people use the first verbose version and I wonder is it worth it?

user3111311
  • 7,583
  • 7
  • 33
  • 48
  • 3
    `std::vector` doesn't even compile. Where did you see that version? –  Jan 28 '14 at 21:13
  • actually you can create vector try it. I use C++11. – user3111311 Jan 28 '14 at 21:14
  • I tried it. Actually I tried it a few days ago, and again just before you posted your comment. Here's mine: http://ideone.com/Pfk49S –  Jan 28 '14 at 21:16
  • @user3111311 1) depends on the compiler 2) if you can, you cant modify it in any way – Sebastian Hoffmann Jan 28 '14 at 21:17
  • 'Verbosity' has practically zero cost. 'Read-only' has major benefits. This doesn't appear to be a real question. – user207421 Jan 28 '14 at 21:19
  • That is strange it works in Visual Studio 2013 – user3111311 Jan 28 '14 at 21:20
  • 1
    @EJP Excessive verbosity has costs too. Even if you don't believe it necessarily impacts the engineers, it does annoy many of them ;-) –  Jan 28 '14 at 21:20
  • @user3111311 (Bad) luck on your part, I guess. The standard doesn't allow it, as the [element type must be copy assignable](http://en.cppreference.com/w/cpp/container/vector). –  Jan 28 '14 at 21:21
  • @delnan That's a benefit, not a cost :-) – user207421 Jan 28 '14 at 21:21
  • It is worthy as void fn (const int) and as useless a const int fn() –  Jan 28 '14 at 21:25
  • @delnan thats only partially true, there was a discussion about it here on SO; the standart only states that certain operations can require the element type to be MoveAssignable (c++11), it does however not state that a `std::vector` with a type not being MoveAssignable cannot be instantiated. Im not sure about this but I guess that implementation dependent. – Sebastian Hoffmann Jan 28 '14 at 21:25
  • @Paranaix So, for certain values of "doesn't allow it"... ;-) But I figure even implementation-defined behaviour would be silly to rely on, especially when it adds no value (AFAIK you can't mutate the elements anyway when the container itself is `const`). –  Jan 28 '14 at 21:26
  • @delnan Also see: http://coliru.stacked-crooked.com/a/4091ca36fad2a944 and compare with: http://coliru.stacked-crooked.com/a/bf68af09cc00dabc – Sebastian Hoffmann Jan 28 '14 at 21:27
  • @delnan I totally agree with you, but thats another story ^^ – Sebastian Hoffmann Jan 28 '14 at 21:27
  • @DavidRodríguez-dribeas Just quoting myself "2) if you can, you cant modify it in any way" – Sebastian Hoffmann Jan 28 '14 at 22:36

2 Answers2

0

Not in this case. Declare the parameter type based on the actual types your program uses - const vector<string>& is const transitive to include its contained strings. That is to say, const vector<string>& does not return mutable references to its strings.

Specifically, const vector<const string>& would be a very rare occurrence (i.e. nonexistent in many codebases) and would typically be painful and/or costly to convert to. Use it only where the inputs do not require conversion (e.g. from std::vector<std::string>).

justin
  • 104,054
  • 14
  • 179
  • 226
0

As discussed in the comments const vector<const string>& will likely not compile and if it does it will produce alot of other problems.

If you however really have to gurantee constness you could go with: const vector<const string*>&

Or: const vector<shared_ptr<const string>>&

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77