2016-10-04 5 views
0
#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; 
} 

이 오류는 다음과 같습니다에서이 C++ 템플릿 매개 변수 공제가 잘못 되었습니까?

/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. 

비 작업 템플릿 std::__1::for_each<std::__1::__wrap_iter<const std::__1::sub_match<const char *> *>으로 추론하는 이유를 예를?

param이 추론 될 것이라고 예상했습니다 std:::cmatch 여기서 param 공제가 어떻게 작동하는지 설명해주십시오.

답변

3

std::cmatch은 의 별칭입니다. 별명이 std::csub_matchstd::sub_match<char const*>이 필요합니다.

std::for_each(cm.begin(), cm.end(), [](const std::csub_match &s) { ... } 
+0

제 질문은 'csub_match'가 왜'cmatch'가 아니 었는지입니다. – PnotNP

+0

@NulledPointer : ['std :: match_results :: value_type'이'std :: sub_match '] (http://en.cppreference.com/w/cpp/regex/match_results)이기 때문에. 여기서 'match_results'에 특별한 것은 없습니다. 이것은 반복자가 작동하는 방식입니다. – ildjarn

+0

나는 iterator가 어떻게 작동하는지 아직도 놓치고 있다고 생각한다. 처음에는 아프다. 그러는 동안 아프다는 대답을 받아 들인다. – PnotNP