[Libav-user] g++ 4.7.2 fails to compile av_err2str
"René J.V. Bertin"
rjvbertin at gmail.com
Wed Jan 16 16:09:21 CET 2013
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.
> 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.
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!
More information about the Libav-user