2016-06-30 60 views
1

我卡住瞭如何使用regex_match模板與我自己的內存STL分配器。std :: regex_match與另一個Allocator

這是我的代碼:

FaF::smatch stringResults; 
std::regex expression("expression"); 
std::regex_match(FaF::string-variable, stringResults, expression); 

對於std::matchstd::string我成功了,所以我用它在上面的例子:

namespace FaF 
{ 
    using smatch = std::match_results<std::string::const_iterator, 
              Allocator<std::string::const_iterator>>; 
    using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>; 
} 

我的分配器有一些記錄,我可以清楚地看到,是的,它確實被使用。 當我正確理解cppreference時,std::regex沒有分配器,但std::regex_matchdoes

我的問題:

如何定義 - 根據上述類型 - 在namespace FaF額外的模板,根據std::regex_match這是用我的STL內存分配器?

+0

我不確定我明白你認爲你有什麼問題。 'std :: regex_match'通過稱爲「template argument deduction」的魔法自動從第一個和第二個參數中選擇分配器。這不是你想要的嗎?你覺得你需要什麼額外的定義,爲什麼? –

+0

順便說一句,'std :: match_results '使用它的分配器來分配'std :: sub_match '的實例,而不僅僅是'Iter',因爲你的typedef表明你相信。 –

+0

@IgorTandetnik 1)我已經懷疑我還必須擴展'Iter'項目。 2)我不認爲'std :: regex_match'確實使用了相同的分配器,因爲我的日誌顯示我被稱爲5次,'stringResults'有5個項目。我將它也更改爲4,並且日誌顯示了4個分配。所以我問了這個問題,因爲對我來說很顯然沒有「模板論證扣除」。 –

回答

0

研究std::regex_match的定義regex.h

template<typename _Ch_traits, typename _Ch_alloc, 
     typename _Alloc, typename _Ch_type, typename _Rx_traits> 
    inline bool 
    regex_match(const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s, 
     match_results<typename basic_string<_Ch_type, 
     _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m, 
     const basic_regex<_Ch_type, _Rx_traits>& __re, 
     regex_constants::match_flag_type __flags 
     = regex_constants::match_default) 
    { return regex_match(__s.begin(), __s.end(), __m, __re, __flags); } 

後,我理解並意識到,我自己的FaF::string和定義我自己FaF::smatch定義[定義的問題]足夠,因爲使用了_Alloc那裏。

我的代碼是這樣的:

void getBarDataEntryFromMySql(const FaF::string & memcacheBarDataEntry) 
{ 
    const char expressionString [] = "expression"; 

    FaF::smatch stringResults; 
    std::regex expression(expressionString); 
    std::regex_match(memcacheBarDataEntry, stringResults, expression); 

    ... 
} 

和它的作品。我想太複雜了......

0

根據cppreference.comregex_match第二個參數應該是

std::match_results<typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc> 

其中第一個參數是一個std::basic_string<CharT,STraits,SAlloc>

這需要你的別名聲明中FaF喜歡有點像:

namespace FaF 
{ 
    using string_type = std::basic_string<char, std::char_traits<char>, 
          Allocator<char>>; 
    using match_type = std::match_results<string_type::const_iterator, 
          Allocator<string_type::const_iterator>>; 
} 

纔能有

  • 結果和
  • 字符串中的字符通過你的分配器分配和
  • 正確參數。
+0

'std :: regex_match'不分配任何「字符串」; 「SAlloc」不用於任何事情。 'std :: regex_match(string,...)'只是簡單地轉過頭來調用'std :: regex_match(string.begin(),string.end(),...)'。後者不再具有'SAlloc',但只有一個分配器,用於分配'std :: sub_match'實例。 –

+0

@IgorTandetnik:'SAlloc'用於分配字符串的字符。傳入有問題的重載。 OP顯然希望自定義分配器同時適用於'string'和迭代器。 – Pixelchemist

+0

'std :: regex_match'雖然沒有分配任何字符串。它的'basic_string'參數是'const'。它的輸出是一個'std :: sub_match'對象的列表,每個對象都將一對迭代器放入原始字符串中。 –

相關問題