[Libav-user] g++ 4.7.2 fails to compile av_err2str
Alex Cohn
alexcohn at netvision.net.il
Wed Jan 16 16:30:49 CET 2013
On Wed, Jan 16, 2013 at 5:09 PM, "René J.V. Bertin" <rjvbertin at gmail.com> wrote:
> On Jan 16, 2013, at 09:45, Alex Cohn wrote:
>
>> The difference is that compound literal is resolved at compile time,
>> so using it inside a loop does not produce extra instances.
>
> Evidently, as long as this is not a decision that's left to the compiler.
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf 6.5.2.2.15
suggests that this decision is carved in stone.
>> Here is my C++ way of defining av_err2str:
>>
>> #ifdef __cplusplus
>>
>> static const std::string av_make_error_string(int errnum)
>> {
>> char errbuf[AV_ERROR_MAX_STRING_SIZE];
>> av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
>> return (std::string)errbuf;
>> }
>>
>> #undef av_err2str
>> #define av_err2str(errnum) av_make_error_string(errnum).c_str()
>>
>> #endif // __cplusplus
>
> Almost exactly as I also would have done it, or else using a specific new class containing a modifiable buffer of size AV_ERROR_MAX_STRING_SIZE to avoid the local variable and copying the string.
People who use C++ and STL have enough classes to take care of, it
would be cruel to impose yet another one on them. I decided to provide
an implementation that could be used as close as possible to the
original C usage pattern.
> But I have a doubt (I'm far from a C++ expert) ... what's the scope of the object returned as return (std::string)errbuf? AFAIK C++ doesn't have a retain/release mechanism like ObjC has, so is it the compiler that keeps track of when to deconstruct the returned string object? In C, returning a local string variable comes without warranty that the contents will still be available when the caller wants to access them!
Sure, you cannot pick the address of the returned object and save it
for future use. Just as with the case currently in error.h for C, the
result should be consumed immediately. If you need to keep it, you can
naturally write
std::string keep_result = av_make_error_string(42);
Enjoy,
Alex Cohn
More information about the Libav-user
mailing list