Assume that I have the following bunch of files:
Generic.h: Complicated template class
#pragma once
template<typename K, typename V, template<typename Key, typename Value, typename ...> typename C>
struct GenericMap
{
C<K, V> key;
};
Special.h: Define fully specialized version of mentioned template class, simplifying the ease of use.
#pragma once
#include "Generic.h"
#include <string>
#include <map>
typedef GenericMap<std::string, int, std::map> SpecialMap;
Client.h: Client that uses SpecialMap and define forward declaration.
#pragma once
class SpecialMap; // Wrong forward declaration
struct Client {
Client();
SpecialMap* map;
};
Client.cpp: Clients code might know Generic.h and Special.h
#include "Client.h"
#include "Special.h"
Client::Client()
{
map["343"] = 2;
}
main.cpp:
#include <Client.h>
int main(int argc, char**args) {
Client c;
return 0;
}
GenericMap represents a template class that has no forward declaration. For some users a fully specialized version SpecialMapof GenericMap should suffices, where for the ease of use a typedef is used.
Now Client uses internally SpecialMap, but the header file should only declare a forward declaration for SpecialMap.
Unfortunately, the following files will not compile. Somehow the posted forward declaration will suffice. What will be the correct one?
I'm sorry for the long listings, but it was the smallest non-working example I could think of.