Boost.Xpressive のセマンティックアクションで整数を受け取る

as を使用します。

[ソース]

#include <boost/xpressive/xpressive.hpp>
#include <boost/xpressive/regex_actions.hpp>

int
main(){
    namespace x = boost::xpressive;
    using x::_;
    
    int n = 0;
    x::sregex regex = ("(" >> (x::s1 = -*_) >> ")")[
        x::ref(n) = x::as<int>(x::s1)
    ];

    std::string const source = "(1234)";
    if( x::regex_search(source, regex) ){
        std::cout << n << std::endl;
    }
    
    return 0;
}

[出力]

1234

[boost]

  • ver 1.49.0