2016-10-04 2 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; 
} 

L'erreur est comme suit:Est-ce que ce dédoublement param est incorrect?

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

Dans l'exemple non-travail pourquoi modèle déduit que std::__1::for_each<std::__1::__wrap_iter<const std::__1::sub_match<const char *> *>?

Je m'attendais à ce que param soit déduit à être std:::cmatch Pouvez-vous expliquer comment la déduction param fonctionne ici?

Répondre

3

std::cmatch est un alias pour std::match_results<char const*>; vous voulez std::sub_match<char const*>, dont l'alias est std::csub_match.

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

Ma question est pourquoi son 'csub_match' pourquoi pas' cmatch'? – PnotNP

+0

@NulledPointer: Parce que ['std :: match_results :: value_type' est' std :: sub_match '] (http://www.cppreference.com/w/cpp/regex/match_results). Ici, il n'y a rien de spécial à propos de 'match_results', c'est comme ça que fonctionne n'importe quel itérateur. – ildjarn

+0

Je pense que je manque encore comment Iterator fonctionne. Je regarde d'abord ça. En attendant, j'accepte votre réponse – PnotNP