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
相关推荐
友友马28 分钟前
『QT』事件处理机制详解 (一)
开发语言·qt
q***441529 分钟前
Spring Security 新版本配置
java·后端·spring
o***741738 分钟前
Springboot中SLF4J详解
java·spring boot·后端
孤独斗士41 分钟前
maven的pom文件总结
java·开发语言
confiself42 分钟前
通义灵码分析ms-swift框架中CHORD算法实现
开发语言·算法·swift
1024小神44 分钟前
在 Swift 中,self. 的使用遵循明确的规则
开发语言·ios·swift
Swift社区1 小时前
Swift 类型系统升级:当协议遇上不可拷贝的类型
开发语言·ios·swift
chengpei1471 小时前
I²C协议简介
c语言·开发语言
唐古乌梁海1 小时前
【IT】常见计算机编程语言多继承问题
开发语言
雨中散步撒哈拉1 小时前
18、做中学 | 初升高 | 考场一 | 面向过程-家庭收支记账软件
开发语言·后端·golang