简体   繁体   English

regex_match 可以访问和不访问已删除 function 的案例

[英]regex_match cases that do & don't access deleted function

I'm trying to understand why one line of my code compiles one way and another line compiles another way.我试图理解为什么我的代码的一行以一种方式编译而另一行以另一种方式编译。

I can't pass the direntry.path().wstring() to regex_match directly without getting an "attempting to reference a deleted function" build error.我无法将 direntry.path().wstring() 直接传递给 regex_match ,而不会出现“尝试引用已删除的函数”构建错误。

const std::wstring directoryPath; // function parameter
for (auto& direntry : std::filesystem::directory_iterator(directoryPath))
{
    std::wsmatch fileMatches;
    if (direntry.is_regular_file()
        // Get "attempting to reference a deleted function" on the next line
        && std::regex_match(direntry.path().wstring(), fileMatches, *m_pRegexFile)
        && fileMatches.size() >= 2)
    {
        ...
    }

Same thing compiles OK if I add the filePath variable.如果我添加filePath变量,同样的东西可以编译。

const std::wstring directoryPath; // function parameter
for (auto& direntry : std::filesystem::directory_iterator(directoryPath))
{
    std::wsmatch fileMatches;
    std::wstring filePath = direntry.path().wstring();
    if (direntry.is_regular_file()
        && std::regex_match(filePath, fileMatches, *m_pRegexFile)
        && fileMatches.size() >= 2)
    {
        ...
    }

Meanwhile this works just fine as-is.同时,这按原样工作得很好。

std::wregex regexSubdirectory(L"cam.*", std::regex::icase);
for (auto& direntry : std::filesystem::directory_iterator(baseFolder.c_str()))
{
    if (direntry.is_directory()
        && std::regex_match(direntry.path().wstring(), regexSubdirectory))
    {
        ...
    }
}

My code is compiling, but I don't like mysteries.我的代码正在编译,但我不喜欢神秘。 Why does one compile without the variable and the other require it?为什么一个编译没有变量而另一个需要它? Best I can tell, one is probably passing const CharT* under the hood, while the other is passing const std::basic_string&.我能说的最好的,一个可能是在引擎盖下传递 const CharT*,而另一个是传递 const std::basic_string&。 But... I am scratching my head at the definitions ( https://en.cppreference.com/w/cpp/regex/regex_match ) and I think they should both be passing the same thing.但是......我对定义( https://en.cppreference.com/w/cpp/regex/regex_match )摸不着头脑,我认为他们都应该通过同样的事情。 Any ideas?有任何想法吗?

Look at #7 in the list on that cppreference page.查看该 cppreference 页面上列表中的 #7。 The call that takes an rvalue basic_string is marked as deleted.采用右值basic_string的调用被标记为已删除。

In your first call, you are passing a temporary string.在您的第一次通话中,您传递了一个临时字符串。 (rvalue) (右值)

In your second call, you are passing an lvalue.在您的第二次通话中,您传递了一个左值。

[Later] The reason that that call is deleted is to prevent a programming error. [稍后] 删除该调用的原因是为了防止编程错误。 Consider the following bit of code from your example:考虑您示例中的以下代码:

std::regex_match(direntry.path().wstring(), fileMatches, *m_pRegexFile);

When this returns, fileMatches contains a bunch of iterators into the string that it matched against.当它返回时, fileMatches包含一堆迭代器到它匹配的字符串中。 However, the string is a temporary, and once regex_match returns, it will be destroyed.但是,字符串是临时的,一旦regex_match返回,就会被销毁。 Now accessing any of the matches in fileMatches will be undefined behavior - and probably a crash.现在访问fileMatches中的任何匹配项都将是未定义的行为 - 并且可能会导致崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM