> 技术文档 > C++ 中重载函数右值引用和左值引用匹配的优先级

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


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

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

std::sregex_iterator it_begin(html.begin(), html.end(), std::regex{R\"raw(\"heading\">.\")raw\"});

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

 regex_token_iterator(_BidIt, _BidIt, const regex_type&&, int = 0, regex_constants::match_flag_type = regex_constants::match_default) = delete;

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

 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); }

验证:

static voidvalue_function(const int32_t &&ir){ (void)wprintf_s(L\"rvalue: %d\\n\", ir);}static voidvalue_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);}

输出结果是:

rvalue: 1lvalue: 2