2014-06-17 40 views
0

我試圖在Visual Studio Ultimate 2012中編譯下面的代碼。它給我一個錯誤,說我打電話過載不存在模板regex_search()。std :: regex_search()不接受我的論據

#include <regex> 

struct Token 
{ 
    //lexertl token wrapper... 
}; 

class Lexer 
{ 
    //... 
    Token &curr; 
    bool skipUntil(const std::regex &regexp); 
}; 

bool Lexer::skipUntil(const std::regex &regexp) 
{ 
    std::smatch m; 
    const char *str = curr.results.start._Ptr; //compiles 
    //ERROR ON NEXT LINE (overload doesn't exist, but it should...) 
    if(std::regex_search(str, regexp, m)) { 
     curr.results.start = m[0].first; 
     curr.results.end = curr.results.start; 
    } 
} 

這是我試圖使用,其據我可以告訴所在的模板......

//from <regex> 
template <class charT, class Alloc, class traits> 
    bool regex_search (const charT* s, match_results<const charT*, Alloc>& m, 
    const basic_regex<charT,traits>& rgx, 
    regex_constants::match_flag_type flags = regex_constants::match_default); 

我知道一個簡單的辦法是隻轉換爲const char *到一個std :: string,但是這對於一個操作來說太昂貴了。

回答

3

regex_search的參數傳遞順序錯誤。第二個參數應該是std::match_results,以及第三個std::basic_regex

而且,當regex_search的第一個參數是std::string時,使用std::smatchstd::match_results<std::string::const_iterator>。由於您傳遞的是char const *,因此您必須使用std::cmatch(或std::match_results<const char*>)。下面的代碼編譯。

char const *str = ""; 
std::cmatch m; 
std::regex regexp; 
std::regex_search(str, m, regexp); 

const char *str = curr.results.start._Ptr; 

線之上看起來很可疑。如果curr.results.start是C++標準庫中的某種類型,那麼肯定不應該訪問那個_Ptr成員,它應該是一個實現細節。使用它會使你的代碼不可移植;當您升級到VS2013時甚至可能會中斷。

+0

cmatch似乎已經解決了我的問題!標記爲答案在一分鐘左右... – ffhighwind

+0

_Ptr是有點iffy。我不確定它是一個特定於Visual Studio的內部表示還是僅僅是一個lexertl事物。我相信所有迭代器在VS中都有_Ptr。 – ffhighwind

+0

@ffhighwind如果這是一個迭代器,那麼它絕對是一個實現細節。標準庫中帶有下劃線後跟大寫字母的任何內容都不適合公共使用。您應該能夠通過使用'&(* curr.results.start)' – Praetorian

相關問題