|
Boost : |
Subject: Re: [boost] [program_options] Some methods take const char*, others take std::string
From: Christopher Jefferson (chris_at_[hidden])
Date: 2011-08-15 12:49:34
On 15 Aug 2011, at 07:57, Yakov Galka wrote:
> It's not just for saving typing, it's for making the interface more generic.
> Consider the following:
>
> void f(const char*);
> void f(const std::string&);
>
> I can use it for null-terminated strings and for std::srings just fine. But
> consider the case I want to pass it a sub-string of some string, or a string
> stored in std::vector, or a non-null terminated string:
>
> std::string str1 = ...;
> std::vector<char> str2 = ...;
> char str3[1024];
> GetBinaryPacket(str3);
>
> f(str1.substr(1, 5)); // unnecessary copy
> f(std::string(str2.begin(), str2.end())); // unnecessary copy
> f(std::string(str3, str3 + 5)); // unnecessary copy
>
> Compare it with:
>
> typedef iterator_range<const char*> str_ref;
> void f(str_ref);
>
> f(str_ref(str1.data()+1, str1.data()+6)); // no copy
> f(str_ref(&str2[0], &str2[0] + str2.size())); // no copy
> f(str_ref(str3, str3 + 5)); // no copy
Is worrying about high efficiency, and lack of copying, in program_options really that important? I would prefer the simplest possible interface, even if it has some small inefficiencies.
Chris
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk