【问题标题】:Regex search won't accept parameter [duplicate]正则表达式搜索不接受参数[重复]
【发布时间】:2020-01-29 08:06:14
【问题描述】:

有人能解释一下为什么在下面的代码中有一行会产生错误吗? 为了比较,下一行编译正确。 由于某些原因,转换为 std::string 与提供 std::string 类型变量不同。 我正在使用带有默认选项的 Visual Studio 2019、C++、控制台应用程序。

#include <iostream>
#include <regex>

int main()
{
    std::regex e;
    std::smatch sm;
    std::string str("abc");
    std::regex_search(std::string("abc"), sm, e);  // Doesn't compile
    std::regex_search(str, sm, e);  // Compiles correctly
}

这是预编译器给出的错误 对不起意大利人...

Gravità Codice Descrizione Progetto File Riga Stato eliminazione Errore (attivo) E1776 impossibile fare riferimento a funzione "std::regex_search(const std::basic_string<_elem _sttraits _stalloc> &&, std::match_results::const_iterator, _Alloc> &, const std::basic_regex<_elem _rxtraits> &, std::regex_constants::match_flag_type = std::regex_constants::match_default) [con _StTraits=std::char_traits, _StAlloc=std::allocator, _Alloc=std::allocator, std::_String_iter_types>>> >>, _Elem=char, _RxTraits=std::regex_traits]" (dichiarato alla riga 2300 di "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include \正则表达式”)。 È una funzione eliminata ConsoleApplication3 C:\Users\munarid\source\repos\Unit-Test-Generation_Support\ConsoleApplication3\ConsoleApplication3.cpp 9

【问题讨论】:

  • 您应该在问题中包含编译器的错误消息:)
  • 如果你访问过这个web,你会在你想调用的重载上看到= delete。所以它不编译也就不足为奇了。

标签: c++ regex


【解决方案1】:

该重载已在 C++ 2014 中删除。

smatch 结果是一个字符串迭代器,一旦函数返回,临时参数对象的迭代器就会失效。

【讨论】:

  • 好的,好的。对于我对 C++ 的有限知识,我深表歉意。但是,我的疑问仍然存在……为什么 std::string 变量可以而强制转换不正确?
  • 很好。我开始看到曙光...在第 7 点查看 en.cppreference.com/w/cpp/regex/regex_search)----- 7)禁止重载 3 接受临时字符串,否则此函数将使用立即变为无效的字符串迭代器填充 match_results m。- --- 所以问题是临时变量。转换产生一个临时变量,而不是“命名”变量不是临时的。好的,谢谢大家的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2016-04-21
  • 2019-08-12
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
相关资源
最近更新 更多