Markus Donath
unread,Apr 1, 2020, 11:20:04 AM4/1/20You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
Hallo,
ich bekomme von einem Compiler (CLANG) eine für mich unverständliche
Warnung:
instantiation of variable 'dict<type1>::intern_map' required here, but
no definition is available
dict.h:
template<typename T>
struct dict {
static std::map<T, std::string> create_intern_map();
static const std::map<T, std::string> intern_map;
};
template<typename T>
std::string str(T t) {
std::string res;
typename std::map<T, std::string>::const_iterator p =
dict<T>::intern_map.find(t);
if(p != dict<T>::intern_map.end())
res = p->second;
return res;
}
enum class type1 : unsigned { t10, t11, t12, t13 };
enum class type2 : unsigned { t20, t21, t22, t23 };
dict.cpp:
template<> std::map<type1, std::string> dict<type1>::create_intern_map()
{
static std::map<type1, std::string> m {
{ type1::t10, "T1.0" },
{ type1::t11, "T1.1" },
{ type1::t12, "T1.2" },
{ type1::t13, "T1.3" }
};
return m;
}
template<> const std::map<type1, std::string> dict<type1>::intern_map =
dict<type1>::create_intern_map();
template<> std::map<type2, std::string> dict<type2>::create_intern_map()
{
static std::map<type2, std::string> m {
{ type2::t20, "T1.0" },
{ type2::t21, "T2.1" },
{ type2::t22, "T2.2" },
{ type2::t23, "T2.3" }
};
return m;
}
template<> const std::map<type2, std::string> dict<type2>::intern_map =
dict<type2>::create_intern_map();
main.cpp:
void test() {
std::string s1 = str(type1::t11);
std::string s2 = str(type2::t21);
std::cout << s1 << ';' << s2 << std::endl;
}
Die Warnung kommt in der str-Funktion beim ersten Aufruf von
dict<T>::intern_map.
Ich bekomme noch den Tip:
add an explicit instantiation declaration to suppress this warning if
'dict<type1>::intern_map' is explicitly instantiated in another
translation unit
aber ich weiß nicht, wie ich das machen kann.
Der msvc-Compiler gibt übrigens keine Warnung.
Markus