// a template function that takes an array of char
// and returns a std::string constructed from it
//
// This function safely 'converts' the array to a pointer
// to it's first element, just like the compiler would
// normally do, but this should avoid diagnostic messages
// from very restrictive lint settings that don't approve
// of passing arrays to functions that expect pointers.
template <typename T, size_t N>
std::string str( T (&arr)[N])
{
return std::string(&arr[0]);
}
使用上面的模板函数,你应该能够像这样通过 linter:
_IDs[str("key")] = str("value");
顺便说一句- 我很惊讶 lint 并没有抱怨这_IDs
是一个保留名称 - 你应该避免在 C 或 C++ 中使用前导下划线,尤其是与大写字母一起使用时。