Using regex_search from the C++ regex library - c++

The regex_search function isn't quite behaving as expected.
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main()
{
string str = "Hello world";
const regex rx("Hello");
cout << regex_search(str.begin(), str.end(), rx) << endl;
return 0;
}
The output is
0
What's going on?

As pointed out in comments to the question, older implementations of the C++ standard libraries did not yet support all features in C++11. Of course, libc++ being an exception because it was originally built specifically for C++11.
According to this bug report support for <regex> in libstdc++ was only implemented for version 4.9 of GCC. You can check the current status on the libstdc++ status page.
One can confirm, that your example works with GCC 4.9 while still failing with GCC 4.8.

Related

Why is charconv header missing in macosx Mojave 10.14 with gcc5?

I am trying to do some very fast conversions in C++ and charconv seems the way to go since it uses a very low level logic. The problem is that when I try to include this header and then call, say, std::to_chars(...), neither the header is found nor std has a 'to_chars' member. I updated and reinstalled gcc but this problem is still there. Now I have seen some threads that say that I should update somehow libc++17 but they are not very specific about what I should do, as things are a bit different for MacOS.
Some code to illustrate the library and it's use:
#include <iostream>
#include <typeinfo>
#include <charconv> //error: 'charconv' file not found
struct to_chars_result{
char *str;
std::errc err;
};
int main(int argc, const char * argv[]) {
std::string str("12Test");
auto result = std::to_chars(str.data(), str.data()+str.size(), 12345); //No
//member named 'to_chars' in namespace 'std'.
return 0;
}
As for gcc -v command output:
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin14.4.0/5.1.0/lto-
wrapper
Ziel: x86_64-apple-darwin14.4.0
Konfiguriert mit: ../gcc-5.1.0/configure --enable-languages=c++,fortran
Thread-Modell: posix
gcc-Version 5.1.0 (GCC)
Any help appreciated!
gcc 5.1 was released on April 22, 2015.
The paper that added to_chars to the C++17 standard was written in 2016.
Why do you expect that gcc5 will have implemented it?
[ Later: That was the paper that added the <charconv> header, too ]

Is this simple C++ program using <locale> correct?

This code seemed to work ok in (ubuntu trusty) versions of gcc and clang, and in Win 7 on a VM via mingw... Recently I upgraded to Wily and builds made with clang crash consistently here.
#include <iostream>
#include <locale>
#include <string>
int main() {
std::cout << "The locale is '" << std::locale("").name() << "'" << std::endl;
}
Sometimes its a gibberish string followed by Aborted: Core dumped and sometimes its invalid free.
$ ./a.out
The locale is 'en_US.UTF-8QX�у�X�у����0�����P�����\�(��\�(��\�(��h��t�������������y���������ț�ԛ�������en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_UP����`�������������������������p�����������#��������������`�������������p��������������������#��#��#��`��������p������������0��P��p���qp��!en_US.UTF-8QЈ[�����\�(��\�(��\�(�����������#�� �����P�����0�����P�����\�(��\�(��\�(��Ȣ�Ԣ����������������(��4��#��L��en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8!�v[��������������#�� �����P�����0�����P�����\�(��\�(���(��h��t��������������������Ȥ�Ԥ�������en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8!��[�� ����[�������7����7��.,!!x�[��!��[��!�[��#�����������#�� �����P�����0�����P�����\�(��\�(��\�(��(��4��#��L��X��d��p��|������������n_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8ѻAborted (core dumped)
$ ./a.out
The locale is 'en_US.UTF-8QX\%�QX\%�Q�G�0H��H�PI��I�\:|�Q\D|�Q\>|�QhK�tK��K��K��K��K��Q�K��K��K��K��K��K�en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8en_US.UTF-8ѻ
*** Error in `./a.out': free(): invalid pointer: 0x0000000000b04a98 ***
Aborted (core dumped)
(Both program outputs above were abbreviated greatly or they would not fit in this question.)
I also got an invalid free on Coliru with it as well.
But this is very similar to example code on cppreference:
#include <iostream>
#include <locale>
#include <string>
int main()
{
std::wcout << "User-preferred locale setting is " << std::locale("").name().c_str() << '\n';
// on startup, the global locale is the "C" locale
std::wcout << 1000.01 << '\n';
// replace the C++ global locale as well as the C locale with the user-preferred locale
std::locale::global(std::locale(""));
// use the new global locale for future wide character output
std::wcout.imbue(std::locale());
// output the same number again
std::wcout << 1000.01 << '\n';
}
Actually that code crashes Coliru also... :facepalm:
More crashes of similar code from Coliru.
Is this a bug in the c++ library used by clang, or is this code defective?
Note also: These crashes seem to be restricted to the C++ api, if you use <clocale> instead things seem to work okay, so it may just be some trivial problem in the C++ bindings over this?
Variations using setlocale: 1 2 3
Looks like this is caused by libstdc++'s ABI change in its basic_string, which was needed for C++11 conformance. To manage this transition, GCC added the abi_tag attribute, which changes the mangled name of functions so that functions for the new and old ABI can be distinguished, even if the change wouldn't otherwise affect the mangled name (e.g. the return type of a function).
This code
#include <locale>
#include <string>
int main() {
std::locale().name();
}
on GCC emits a call to _ZNKSt6locale4nameB5cxx11Ev, which demangles to std::locale::name[abi:cxx11]() const, and returns a SSO string with the new ABI.
Clang, on other other hand, doesn't support the abi_tag attribute, and emits a call to _ZNKSt6locale4nameEv, which demangles to simply std::locale::name() const - which is the version returning a COW string (the old ABI).
The net result is that the program ends up trying to use a COW string as an SSO string when compiled with Clang. Havoc ensues.
The obvious workaround is to force the old ABI via -D_GLIBCXX_USE_CXX11_ABI=0.
I think the "" parameter might be corrupting something. I don't think it's a legal argument?
To verify it's nothing else, try running this:
#include <iostream>
#include <locale>
int main() {
std::locale("").name();
}
It compiles and runs just fine with GCC:
g++ -Wall -pedantic locale.cpp
<= No errorrs, no warnings
./a.out
The locale is 'en_US.UTF-8'
<= Expected output
ADDENDUM:
Exactly the same with MSVS 2013 - no errors or warnings compiling; no errors running:
locale.cpp =>
#include <iostream>
#include <locale>
#include <string>
int main() {
std::cout << "The locale is '" << std::locale("").name() << "'" << std::endl;
}
Output =>
locale
The locale is 'English_United States.1252'

Can I use stoi with GCC 4.4.7 compiler?

I don't want to use -std=c++11.
Is there another way ?
I have this peace of code:
#include <iostream>
using namespace std;
#include <string>
#include <sstream>
int main()
{
std::string str1 = "45";
int myint1 = std::stoi(str1); <--error: Function 'stoi' could not be resolved
}
stoi is from C++11, so if you don't want to use flag -std=c++11 you cannot use stoi (or you can wait until gcc 6.0 will be released, where -std=gnu++14 will be default standard).
But you can use something else: boost::lexical_cast, some manually written function, etc.
If you question is "Can I use stoi with GCC 4.4.7 compiler?", then
Yes, you can use std::stoi with -std=c++0x. GCC 4.4 does not have much of support for fancy C++11 features, but it does support simple C++11 functions like std::stoi. Check this support table out for GCC 4.4's support for C++0x.

(Software issue) Can anyone suggest a download that will definitely bring my Mingw32 compiler up to speed?

#include <iostream>
#include <string>
using namespace std;
int main(){
string s = "wassup", newVal = "though";
cout << *(s.insert(s.begin(), newVal.begin(), newVal.end()));
}
This brings up the problem that the return type of string's insert member function is void (error: void value not ignored as it ought to be). This link indicates C++98 returns void but the "new" standard C++11 does indeed return an iterator.
A bit of context, I actually faced this problem earlier. I was/am using CodeBlocks (GCC Compiler Collection) on Windows 7 64-bit and this program gave the same issue (dereferencing void):
#include <iostream>
#include <list>
int main(){
list<int> x = {1,2,3,4};
*(x.insert(++x.begin(), 3, 2));
for(auto c : x)
cout << c;
}
I posted my issue on a different forum and a user pointed out Mingw32 was missing that particular C++11 change, indicating Mingw-w64 does not have this issue. So I went straight to installing Mingw-w64 on Code::Blocks using this guide and the problem was resolved. It's only now I've found out that the overloaded function which takes three iterator parameters STILL returns void.
I'm a little confused as to why Code::Blocks Mingw32 didn't supply a fully updated C++11 standard. Can anyone suggest a download that will definitely bring my compiler up to speed?

g++4.9 libstdc++ std::string support for C++11 broken

Does anyone know if libstdc++ in g++ <= 4.9 is fully C++11-compliant (or claimed to be)? For example, the code below does not compile (compiles on clang):
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
// g++4.9 bug, returns void
// instead, according to the standard N3936 21.4.6.4/21
// should return an iterator (string::iterator or wrapper)
auto x = s.insert(s.begin(), 10,'A');
cout << s << endl;
}
The overload of std::string::insert I'm using is declared in 21.4.6.4/21 (N3936) as returning an iterator to the first character inserted
iterator insert(const_iterator p, size_type n, charT c);
however in the code above it returns void. Is the std::string part of the library non-compliant? I wonder if it compiles on g++4.10.
N3936 is a C++14 draft, for C++11 the closest draft standard would be N3337 but in this case it still has a return type of iterator so the issue is the same.
The link that Praetorian provides in his comment does not seem to have the information anymore and I can not find a specific bug report for this case but in a related case where libstdc++ and libc++ disagree: Does C++11 require allocators to be default constructible, libstdc++ and libc++ disagree? the issue is the same.
libstdc++ implementation of std::string is not fully C++11 compliant but in this bug report we can see that with gcc 5.0 release this should be fixed:
Fixed for GCC 5 (when using the new string ABI)
and we can see this live .