Search code examples
c++constantsre2

Complex const declarations in C++


I'm taking my first foray into C++, specifically the Google RE2 library, and I am stuck on some of the syntax. I am trying to invoke a function with the signature:

static bool FindAndConsumeN(StringPiece* input, const RE2& pattern,
                         const Arg* const args[], int argc);

With the code:

const re2::RE2::Arg match;
bool isMatched = RE2::FindAndConsumeN(&inputPiece, *expression,new const re2::RE2::Arg[] { &match },0)

However I am getting the compiler error:

Error   3   error C2664: 're2::RE2::FindAndConsumeN' : cannot convert parameter 3 from 'const re2::RE2::Arg (*)[]' to 'const re2::RE2::Arg *const []'

I've clearly got the datatype of the third argument wrong, but does anyone know what the correct data type is?

I am compiling the code with Visual Studio 2010


Solution

  • You should use code like this:

    re2::RE2::Arg match;
    re2::RE2::Arg* args[] = { &match };
    re2::RE2::FindAndConsumeN(NULL, pattern, args, 1);
    

    args will be converted to const Arg* args[].

    The inner const has no deal with calling code, it works only within FindAndConsumeN.

    Don't use new because you can't delete array later.
    (with new it would be new const re2::RE2::Arg*[])