Thread: Templates

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    2

    Templates

    Code:
    template <typename T>
    auto RegexSearch(T const* str,
                     T const* regex)
    {
        using iter = T const*;
        std::basic_regex<T> re(regex);
        std::match_results<iter> m;
        std::regex_search(str, m, re);
        return m;
    }
    
    
    auto matches = RegexSearch(L"10,20", LR"((\d+))");
    
    
    // ------------------------------------------------------------- //
    
    
    template <typename T>
    auto RegexSearch(const std::basic_string<T>& str,
                     const std::basic_string<T>& regex)
        {
            using iter = typename std::basic_string<T>::const_iterator;
            std::basic_regex<T> re(regex);
            std::match_results<iter> m;
            std::regex_search(str, m, re);
            return m;
        }
    
    
    auto matches = RegexSearch(L"10,20"s, LR"((\d+))"s);

    what's the difference between passing parameters to the template as

    T const * str <--- this 'method', m return correct values
    and
    const std::basic_string<T>& str <--- this, m return garbage (because the string lives only inside of the template scope(??))

    some questions i got:

    1. there are any 'cons' in passing parameters to templates as i did in the first template?

    2. is the basic_string method 'faster'? why? or theres no difference between these two methods?

    i'm a beginner in c++
    Last edited by c7aesa7r; 08-30-2022 at 05:47 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    The one using basic_string doesn't work because the iterators in the returned match_result point to a temporary object that is created for the RegexSearch function call.
    You can make it work by creating a basic_string object that continues to exist in the calling function, something like:
    Code:
        auto s = L"10,20"s;
        auto matches = RegexSearch(s, LR"((\d+))"s);
    I don't understand your first question.
    For your second question, using basic_string might actually be slower, but not significantly so. The difference is that basic_string objects need to be created for the call.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    2
    I see, do you know how to use the regex_iterator for const char/wchar?


    using cregex_iterator = regex_iterator<const char*>;
    using wcregex_iterator = regex_iterator<const wchar_t*>;
    using sregex_iterator = regex_iterator<string::const_iterator>;
    using wsregex_iterator = regex_iterator<wstring::const_iterator>;
    Code:
    template <typename T>
    auto RegexMatchAll(T const* str, T const* regex)
    {
    
    
         using iter = std::regex_iterator<T>;
         std::basic_regex<T> re(regex);
         auto words_begin = iter(?, ?, re); // <--- how to iterate?
         auto words_end = iter();
    
          std::vector<std::match_results<iter>> result;
    
    
          for (iter i = words_begin; i != words_end; ++i) {
             std::match_results<iter> m = *i;
             result.emplace_back(m);
          }  
    
    
        return result;
    }
    
    auto matches = RegexMatchAll(L"10,20,30", LR"((\d+))");
    how to use the regex iterator in this case that str is a wchar_t*

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    You could do something like this:
    Code:
    auto string_len(char const* s) { return strlen(s); }
    auto string_len(wchar_t const* s) { return wcslen(s); }
     
    template <typename T>
    auto RegexMatchAll(T const* str, T const* regex) {
        using iter = std::regex_iterator<T const*>;
        std::basic_regex<T> re(regex);
        auto words_begin = iter(str, str + string_len(str), re);
        return std::vector<std::match_results<T const*>>(words_begin, iter());
    }
    Note that a couple of places where you used T needed to be T const*.
    Also, I filled the vector by using a constructor that takes iterators.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates and Templates objects
    By Dave11 in forum C++ Programming
    Replies: 6
    Last Post: 07-05-2014, 06:27 AM
  2. Templates in C++
    By flexo87 in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2011, 01:49 PM
  3. Templates: What does this do?
    By NeonBlack in forum C++ Programming
    Replies: 4
    Last Post: 07-14-2011, 08:24 PM
  4. Templates
    By f0r3nsic in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 09:26 AM
  5. Templates
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2003, 03:58 PM

Tags for this Thread