I’ve been given some code that compiles fine on MSVC, and I’m trying to get it to compile on Clang in Xcode. I’m currently coming across an issue where a class is being redefined using the following typedef:
typedef std::map<MyNS::istring, EntityState> Entity;
Looking at the preprocessed output, I can see that there are two forward declarations of class Entity before this typedef. However, the actual definition of class Entity is not in the preprocessed output, but it is in the same namespace as the new Entity map (not MyNS though…). Is it the forward declarations that are causing this error? And is there some way that this could be valid in MSVC and not working due to the pedantry of Clang?
EDIT: I don’t have MSVC to hand, but here’s a snippet I put together to demonstrate the kind of error I’m getting (I’ve simplified the definitions so that it all fits in a small space). This causes the same error as I get when I try to compile it with Clang. Would this work in MSVC?
namespace TheNS {
class Entity;
struct EntityState
{
std::string aString, anotherString;
int anInt;
EntityState() {}
EntityState(std::string a, std::string b, int i)
{
// constructor
}
};
typedef std::map<std::string, EntityState> Entity;
class Entity
{
public:
void SomeFunction();
private:
int m_aVar;
};
}
Yes, it’s not correct. Should never compile, if it compiles on MSVC – probably it’s a bug of compiler. Forward declaration tells compiler, that
TheNS::Entitywill be class and nothing else (not enum, union, or typedef). Really, your code is same asOf course it’s incorrect.
n3337 9.1/2
So, after this
compiles knows, that
Entitywill be used as class-name. This name can be redeclared as function (in the same scope), in this case you should useclass Entity, when you want to useEntityclass (or redeclareEntityname by typedef as says in comments).7.1.3/6