I want to write a conversion utility header, which can be used without much fuss, so it should be used as a header-only library (since it's so small it would be overkill to create a seperate .lib for it anyway).
Here is a sketch of the lib (Containing only UTF8 and UTF16 conversion)
namespace cp_utils {
namespace cp { //codepages
struct UTF8 {
typedef std::string string;
typedef const std::string& string_ref; //will be boost::string_ref later on
template<typename ToCP>
static typename ToCP::string convertTo(string_ref str);
template<>
static UTF16::string convertTo<UTF16>(string_ref str) { //<- Problem here!
//... convert UTF-8 string to UTF-16 wstring
return L"UTF-16"; //Mock implementation
}
};
struct UTF16 {
typedef std::wstring string;
typedef const std::wstring& string_ref;
template<typename ToCP>
static typename ToCP::string convertTo(string_ref str);
template<>
static UTF8::string convertTo<UTF8>(string_ref str) {
//... convert UTF-16 wstring to UTF-8 string
return "UTF-8"; //Mock implementation
}
};
///... other codepages similar
}
//actual conversion function
template<typename FromCP, typename ToCP>
inline typename ToCP::string convert(typename FromCP::string_ref str) {
return FromCP::convertTo<ToCP>(str);
}
}
//usage:
int main() {
wstring utf16str = convert<cp::UTF8, cp::UTF16>("test");
}
The error message says C2065: "UTF16" undeclared identifier
I tried to fix this by a forward declaration of the UTF16 struct with a simple struct UTF16;
, but then the error states C2027: use of undefined type "UTF16". I somehow have to provide the full definition of UTF16 before defining UTF8 and vice-versa.
If I define the specialized conversion functions of UTF8 and UTF16 outside the structs, then it works fine as long as the library is used in only one .cpp file because of multiple definition, which makes this pretty impractial.
How can I solve this problem?
PS: I compiled the code with MSVS 2013