C++ application code was compiling fine with GCC 4.1. Now i upgraded the GCC version to 4.4X and i am getting an error.
error: declaration of ‘data’ as array of references
CODE:
inline std::string base64_encode(const std::vector< unsigned char >& data)
{
if (data.empty())
{
return "";
}
using namespace boost::archive::iterators;
typedef base64_from_binary<
transform_width< const unsigned char*, 6, 8> > base64_enc;
std::string result(base64_enc(&data[0]),
base64_enc(&data[0] + data.size()));
static const std::string base64_padding[] = { "", "==", "=" };
result.append(base64_padding[data.size() % 3]);
return result;
}
I read few answers and came to know that this doesn't comply with vexing parse rule.
One possible solution is to enclose (&data)[0] or std::string result({base64_enc(&data[0]}), base64_enc(&data[0] + data.size();
std::string result(base64_enc(&data[0]),
base64_enc(&data[0] + data.size()));
Compiler should not consider this line as a function declaration. How do i apply the proper grammar in this case ?
One option is to change std::string result(
to std::string result = std::string(
(leave the rest of the line the same).
I think it is a compiler bug because &data[0] + data.size()
could not occur in a declaration, but I have seen similar bugs in gcc before (it mistakenly flags the line as a declaration too early in parsing).
This version also worked around the bug for me in gcc 8.3, adding an extra set of parentheses:
std::string result ( (base64_enc(&data[0])), base64_enc(&data[0] + data.size()) );
// ^ ^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.