C++ 中重载函数右值引用和左值引用匹配的优先级

C++ 中重载函数右值引用和左值引用匹配的优先级

在使用 C++ STL 里的 std::regex 时,遇到了一个问题:

cpp 复制代码
std::sregex_iterator it_begin(html.begin(), html.end(),
                               std::regex{R"raw("heading">.</h2>")raw"});

上面这种做法不行,原因是上面的代码可以匹配到下面代码:

cpp 复制代码
 regex_token_iterator(_BidIt, _BidIt, const regex_type&&, int = 0,
        regex_constants::match_flag_type = regex_constants::match_default) = delete;

但是不能匹配到下面的代码:

cpp 复制代码
 regex_token_iterator(_BidIt _First, _BidIt _Last, const regex_type& _Re, int _Sub = 0,
        regex_constants::match_flag_type _Fl = regex_constants::match_default)
        : _Pos(_First, _Last, _Re, _Fl), _Cur(0), _Subs(&_Sub, &_Sub + 1) {
        _Init(_First, _Last);
    }

验证:

cpp 复制代码
static void
value_function(const int32_t &&ir)
{
  (void)wprintf_s(L"rvalue: %d\n", ir);
}

static void
value_function(const int32_t &il)
{
  (void)wprintf_s(L"lvalue: %d\n", il);
}

int wmain(int argc, wchar_t *argv[])
{
  value_function(1);
  int n{ 2 };
  value_function(n);
}

输出结果是:

cpp 复制代码
rvalue: 1
lvalue: 2
相关推荐
hetao17338371 分钟前
2026-05-25~06-11 hetao1733837 的刷题记录
c++·算法
洛水水6 分钟前
【力扣100题】82.有效的括号
c++·算法·leetcode
码语智行8 分钟前
基于word模板导出人员信息
java
云水-禅心9 分钟前
解决MacOS 安装Python之后默认版本指向不正确问题
开发语言·python·macos
冰暮流星10 分钟前
javascript之this关键字
开发语言·前端·javascript
rit843249910 分钟前
基于Qt的串口上位机控制蓝牙小车程序
开发语言·qt
百度Geek说11 分钟前
CodingAgent 的原始森林困境:一张地图能解决什么?
开发语言·javascript·ecmascript·coding agent
西凉的悲伤16 分钟前
redis和数据库实现分布式锁
java·数据库·redis·分布式
sunny.day19 分钟前
js原型与原型链
开发语言·javascript·原型模式·js原型链