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
相关推荐
only-qi3 小时前
146. LRU 缓存
java·算法·缓存
悟能不能悟3 小时前
js闭包问题
开发语言·前端·javascript
潼心1412o3 小时前
C语言(长期更新)第15讲 指针详解(五):习题实战
c语言·开发语言
xuxie134 小时前
SpringBoot文件下载(多文件以zip形式,单文件格式不变)
java·spring boot·后端
重生成为编程大王4 小时前
Java中的多态有什么用?
java·后端
Murphy_lx4 小时前
Lambda表达式
开发语言·c++
666和7774 小时前
Struts2 工作总结
java·数据库
中草药z4 小时前
【Stream API】高效简化集合处理
java·前端·javascript·stream·parallelstream·并行流
yangpipi-4 小时前
C++并发编程-23. 线程间切分任务的方法
开发语言·c++
野犬寒鸦4 小时前
力扣hot100:搜索二维矩阵 II(常见误区与高效解法详解)(240)
java·数据结构·算法·leetcode·面试