ffhighwind
ffhighwind

Reputation: 165

std::regex_search() isn't accepting my arguments

I'm trying to compile the following code in Visual Studio Ultimate 2012. It's giving me an error saying that I'm calling an overload that doesn't exist for the template 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;
    }
}

This is the template I'm trying to use, which as far as I can tell exists...

//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);

I know an easy fix would be to just convert the const char * to a std::string, but that's too expensive of an operation to consider.

Upvotes: 0

Views: 129

Answers (1)

Praetorian
Praetorian

Reputation: 109119

The arguments to regex_search are being passed in the wrong order. The second argument should be std::match_results, and the third std::basic_regex.

Also, std::smatch, or std::match_results<std::string::const_iterator> is used when the first argument to regex_search is std::string. Since you're passing in a char const *, you must use std::cmatch (or std::match_results<const char*>). The following code compiles.

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

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

The line above looks very suspicious. If curr.results.start is some type from the C++ standard library, you definitely shouldn't be accessing that _Ptr member, it's supposed to be an implementation detail. Using it will make your code non-portable; it might even break when you upgrade to VS2013.

Upvotes: 3

Related Questions