Can I declare and initialize struct separately?
Ref refType(string strRef) {
Ref ref;
if (regex_match(strRef.begin(), strRef.end(), rxIdentifier)) {
ref = { Var, strRef };
} else if (regex_match(strRef.begin(), strRef.end(), rxConstant)) {
ref = { Const, strRef };
}
return ref;
}
Doesn’t appear to work. Visual Studio complains about the { for initializing the struct.
Yes, but only in C99 and C++11. Older versions of C and C++ do not support anything like this.
In C99, they’re called compound literals, and they look like this:
In C++11, they’re called extended initializer lists, and they look like this:
However, as Evgeny Panasyuk pointed out, Visual Studio is not yet fully compliant with the C++11 standard. Visual Studio 2010 was released before the C++11 standard was finalized, and even then it wasn’t fully compliant with the draft standard at the time. Apparently VS2012 hasn’t quite caught up yet, either.
As a result, Visual Studio does not yet support extended initializer lists. Visual Studio also does not support C99, and Microsoft has indicated that they do not intend to support C99 any time soon.