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
相关推荐
程序员泡椒1 小时前
二分查找Go版本实现
数据结构·c++·算法·leetcode·go·二分
瑾修1 小时前
golang查找cpu过高的函数
开发语言·后端·golang
kkkAloha1 小时前
JS笔记汇总
开发语言·javascript·笔记
之歆7 小时前
Spring AI入门到实战到原理源码-MCP
java·人工智能·spring
LawrenceLan7 小时前
Flutter 零基础入门(十一):空安全(Null Safety)基础
开发语言·flutter·dart
yangminlei7 小时前
Spring Boot3集成LiteFlow!轻松实现业务流程编排
java·spring boot·后端
qq_318121597 小时前
互联网大厂Java面试故事:从Spring Boot到微服务架构的技术挑战与解答
java·spring boot·redis·spring cloud·微服务·面试·内容社区
txinyu的博客7 小时前
解析业务层的key冲突问题
开发语言·c++·分布式
J_liaty7 小时前
Spring Boot整合Nacos:从入门到精通
java·spring boot·后端·nacos
码不停蹄Zzz8 小时前
C语言第1章
c语言·开发语言