C++ 我应该使用模板检查字符串和向量的空性吗?

C++ 我应该使用模板检查字符串和向量的空性吗?,c++,C++,我必须检查一个类的成员是否为空。该类正在从json文件读取一些值,如果一些成员为空,则表示json格式错误。到目前为止,我只需要检查字符串的空值,我使用了一个函数: void ConfigFile::checkEmptyness(const std::string& strIn, const std::string& memberNameIn, const std::string& pathIn) { if (strIn.empty()) {

我必须检查一个类的成员是否为空。该类正在从json文件读取一些值,如果一些成员为空,则表示json格式错误。到目前为止,我只需要检查字符串的空值,我使用了一个函数:

void ConfigFile::checkEmptyness(const std::string& strIn, const std::string& memberNameIn, const std::string& pathIn)
{
    if (strIn.empty())
    {
        throw ConfigFileException(emptyStringOrGetFailedMsg(pathIn, memberNameIn));
    }
}
现在我必须修改应用程序,以便从json中读取浮点向量。我发现了如何从json中读取向量:

for (auto& item : tmpPT.get_child(path))
{
    float label = item.second.get_value< float >();
    checkNegativity< float >(label, "classifierSearchedClass", path);
    m_classifierSearchedClass.push_back(label);
}

是一个好方法,还是使用重载更好?

这是一个好方法,因为它遵守了。

什么是编译错误?Worksoops,很抱歉,这是另一个错误(已编辑)。。。不管怎样,这是对的?我想是的,如果以后有不起作用的类型,你可以专门为它们设计
template< typename T>
void ConfigFile::checkEmptyness(const T& arrIn, const std::string& memberNameIn, const std::string& pathIn)
{
    if (arrIn.empty())
    {
        throw ConfigFileException(emptyStringOrGetFailedMsg(pathIn, memberNameIn));
    }
}