本文介绍了这是c ++模板param deduction不正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <iostream>
#include <regex>
int main(void)
{
std::cmatch cm;
std::regex_match("subject", cm, std::regex("(sub)(.*)"));
//std::for_each(cm.begin(), cm.end(), [](const std::sub_match<const char *> &s){ <---- Working statement
std::for_each(cm.begin(), cm.end(), [](const std::cmatch &s){ /*<--- Non-working statement*/
std::cout << "match:" << s.str() <<std::endl;
});
return 0;
}
错误如下:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:853:9: error: no matching function for call to object of type '(lambda at main.cpp:73:41)'
__f(*__first);
^~~
main.cpp:73:10: note: in instantiation of function template specialization 'std::__1::for_each<std::__1::__wrap_iter<const std::__1::sub_match<const char *> *>, (lambda at main.cpp:73:41)>' requested here
std::for_each(cm.begin(), cm.end(), [](const std::match_results<const char*> &s){
^
main.cpp:73:41: note: candidate function not viable: no known conversion from 'const std::__1::sub_match<const char *>' to 'const std::match_results<const char *>' for 1st argument
std::for_each(cm.begin(), cm.end(), [](const std::match_results<const char*> &s){
^
maintool.cpp:73:41: note: conversion candidate of type 'void (*)(const std::match_results<const char *> &)'
1 error generated.
$ b $ p
在非工作示例中,为什么模板被推导为 std :: __ 1 :: for_each< std :: __ 1 :: __ wrap_iter< const std :: __ 1 :: sub_match< const char *> *>
?
我期望param将被推导为 std :::cmatch
你能解释一下param deduction是如何工作的?
I was expecting param will be deduced to be std:::cmatch
Can you please explain how param deduction is working here?
推荐答案
std :: cmatch
是 std :: match_results< char const *>
;您想要 std :: sub_match< char const *>
std :: csub_match
。
std::for_each(cm.begin(), cm.end(), [](const std::csub_match &s) { ... }
这篇关于这是c ++模板param deduction不正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!