‘multiline’ is not a member of ‘std::__cxx11::regex’ - c++

I am trying to compile the following C++ code using the command:
g++ -std=c++17 -o rgx rgx.cpp
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
regex rgx("^([A-Z]*)$", regex::ECMAScript | regex::multiline | regex::icase);
smatch rmtch;
string str = "AbcefgH\r\nTest";
if (regex_match(str, rmtch, rgx)) {
for (size_t i = 0; i < rmtch.size(); i++) {
ssub_match smtch = rmtch[i];
string s_m = smtch.str();
cout << " submatch " << i << ": " << s_m << endl;
}
}
return 0;
};
However get the following compile time error
rgx.cpp: In function ‘int main()’:
rgx.cpp:7:53: error: ‘multiline’ is not a member of ‘std::__cxx11::regex’ {aka ‘std::__cxx11::basic_regex<char>’}
7 | regex rgx("^([A-Z]*)$", regex::ECMAScript | regex::multiline | regex::icase);
g++ --version
g++ (Ubuntu 9.1.0-8ubuntu1) 9.1.0
Why is g++ using __cxx11::regex when I've specified -std=c++17?
cppreference.com/w/cpp/regex/basic_regex defines regex::multiline as being part of the C++17 standard

Libstdc++ doesn't appear to implement regex::multiline.
Note that the fact that it's using std::__cxx11::regex doesn't mean that you're stuck in the past. __cxx11 is not strictly reserved to C++11 features. The purpose of inline namespaces (such as __cxx11) is that if eventually a class/struct needs to change in ways that are not backwards compatible, new programs will grab its definition from a new inline namespace while the old definition remains for programs that were built before.

Related

Apple Clang gives error when using bracket initialization, while g++ and ordinary Clang works

This code example from learncpp.com doesn't work on Apple Clang 13.0.0 while working on both ordinary Clang and g++ from Homebrew.
Code
#include <iostream>
int getValueFromUser()
{
std::cout << "Enter an integer: ";
int input{};
std::cin >> input;
return input;
}
void printDouble(int value)
{
std::cout << value << " doubled is: " << value * 2 << '\n';
}
int main()
{
printDouble(getValueFromUser());
return 0;
}
Error
main.cpp:6:11: error: expected ';' at end of declaration
int input{};
^
;
1 error generated.
Does Apple Clang have no support for initializations by brackets? I'm really puzzled right now.
Yeah, seems like Apple Clang's default standard is super old. When I added -std=c++20 as a flag it compiled.

Incorrect overload resolution in for_each_n? [duplicate]

I have small piece of code for std::for_each_n loop. I tried running it on inbuilt Coliru compiler GCC C++17 using following command :
g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp && ./a.out
But compiler give an error that " 'for_each_n' is not a member of 'std' ".
My code is bellow which is copied from cppreference.
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> ns{1, 2, 3, 4, 5};
for (auto n: ns) std::cout << n << ", ";
std::cout << '\n';
std::for_each_n(ns.begin(), 3, [](auto& n){ n *= 2; });
for (auto n: ns) std::cout << n << ", ";
std::cout << '\n';
}
So, Why I'm getting an error?
There is nothing wrong with your code. The issue is that libstdc++ does not support std::for_each_n until GCC 8 and Clang 8. If we look at the header that defines std::for_each_n, we see it does not exist.
However, if you have access to libc++, their header from the official mirror does implement std::for_each_n.
(Update: the current version of the GCC repository now also does include for_each_n)

std::regex fails to match char

I'm trying to get a regex to match a char containing a space ' '.
When compiled with g++ (MinGW 8.1.0 on Windows) it reliably fails to match.
When compiled with onlinegdb it reliably matches
Why would the behaviour differ between these two? What would be the best way to get my regex to match properly without using a std::string (which does match correctly)
My code:
#include <iostream>
#include <regex>
#include <string>
int main() {
char a = ' ';
std::string b = " ";
cout << std::regex_match(b, std::regex("\\s+")) << \n; // always writes 1
cout << std::regex_match(&a, std::regex("\\s+")) << \n; // writes 1 in onlinegdb, 0 with MinGW
}

'for_each_n' is not a member of 'std' in C++17

I have small piece of code for std::for_each_n loop. I tried running it on inbuilt Coliru compiler GCC C++17 using following command :
g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp && ./a.out
But compiler give an error that " 'for_each_n' is not a member of 'std' ".
My code is bellow which is copied from cppreference.
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> ns{1, 2, 3, 4, 5};
for (auto n: ns) std::cout << n << ", ";
std::cout << '\n';
std::for_each_n(ns.begin(), 3, [](auto& n){ n *= 2; });
for (auto n: ns) std::cout << n << ", ";
std::cout << '\n';
}
So, Why I'm getting an error?
There is nothing wrong with your code. The issue is that libstdc++ does not support std::for_each_n until GCC 8 and Clang 8. If we look at the header that defines std::for_each_n, we see it does not exist.
However, if you have access to libc++, their header from the official mirror does implement std::for_each_n.
(Update: the current version of the GCC repository now also does include for_each_n)

C++ using ldap_bind from ldap.h

I'm trying to use ldap_bind, but get an this error.
error: âldap_bindâ was not declared in this scope
code:
#include <lber.h>
#include <ldap.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
LDAP *ld;
char *ldap_host = "ldap://localhost";
int ldap_port = 389;
int auth_method = LDAP_AUTH_SIMPLE;
int desired_version = LDAP_VERSION3;
char *root_dn = "ou=people,dc=localhost,dc=local";
char *root_ps = "password";
int result;
result = ldap_initialize(&ld, ldap_host);
cout << "result: " << result << endl;
result = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version);
cout << "result: " << result << endl;
result = ldap_bind_s(ld, root_dn, root_ps, auth_method);
cout << "result: " << result << endl;
}
I'm compiling with this command
g++ ldap.cpp -llber -lldap -o prog
TIA
I've no experience with OpenLDAP, but from the header it seems you need:
extern "C" {
# define LDAP_DEPRECATED
# include <ldap.h>
# include <lber.h>
}
It leads to some compiling errors in current version, since in the ldap.h use #if LDAP_DEPRECATED instead of #ifdef, give the MACRO a value:
#define LDAP_DEPRECATED 1
And it is good to go.
Dont use ldap_bind. Its deprecated. Rather use ldap_sasl_bind.
ldap.h has deprecated a lot of functions for mostly security reasons
Check out the following command which lists all the deprecated functions
grep deprecate < /usr/include/ldap.h
On *nix systems, or any system that let's you specify compilation flags, you can add the following to your list of flags:
-DLDAP_DEPRECATED
This allows you to use the deprecated deprecated features without having to add defines to the top of all of your source/header files.