I've got a really strange problem. MSVC doesn't have initializer lists, so I've used a lambda to approximate them.
static const std::unordered_map<std::wstring, LexedFile::Token> reserved_words =
[]() -> std::unordered_开发者_高级运维map<std::wstring, LexedFile::Token> {
std::unordered_map<std::wstring, LexedFile::Token> retval;
// Do stuff with retval
return retval;
}();
MSVC throws a compiler error.
error C2440: 'initializing' : cannot convert from 'int' to
'std::tr1::unordered_map<_Kty,_Ty>'
Unless I'm quite blind, there's no "int" anywhere near this. I don't see what's wrong. Any suggestions?
Edit:
There's nothing funky about // Do stuff with retval
, it's just a bunch of insertions, and this is a function-scope static variable in a lambda in a member function.
auto next = [&] {
static const std::unordered_map<std::wstring, LexedFile::Token> reserved_words =
[]() -> std::unordered_map<std::wstring, LexedFile::Token> {
std::unordered_map<std::wstring, LexedFile::Token> retval;
retval[L"namespace"] = LexedFile::Token::Namespace;
retval[L"for"] = LexedFile::Token::For;
retval[L"while"] = LexedFile::Token::While;
retval[L"do"] = LexedFile::Token::Do;
retval[L"switch"] = LexedFile::Token::Switch;
retval[L"case"] = LexedFile::Token::Case;
retval[L"default"] = LexedFile::Token::Default;
retval[L"try"] = LexedFile::Token::Try;
retval[L"catch"] = LexedFile::Token::Catch;
retval[L"auto"] = LexedFile::Token::Auto;
retval[L"type"] = LexedFile::Token::Type;
retval[L"break"] = LexedFile::Token::Break;
retval[L"continue"] = LexedFile::Token::Continue;
retval[L"return"] = LexedFile::Token::Return;
retval[L"static"] = LexedFile::Token::Static;
retval[L"sizeof"] = LexedFile::Token::Sizeof;
retval[L"decltype"] = LexedFile::Token::Decltype;
retval[L"if"] = LexedFile::Token::If;
retval[L"else"] = LexedFile::Token::Else;
return retval;
}();
if (stack.empty())
return;
std::wstring token(stack.begin(), stack.end());
stack.clear();
if (reserved_words.find(token) != reserved_words.end()) {
l.tokens.push_back(reserved_words.find(token)->second);
return;
}
l.tokens.push_back(LexedFile::Identifier);
};
The compiler will accept it if I use the constructor directly not initialization, which seems very strange. Probably a compiler bug.
Calling the constructor with () instead of using = works just fine, so I'm marking this one as a compiler error.
精彩评论