What about atomic_flag.wait/notify_one in gcc and clang? - c++

I can not find any new c++20 features of std::atomic_flag (Proposal P0995R1)
I looked in gcc status table but did not found anything about it
https://gcc.gnu.org/projects/cxx-status.html
Then i tried compile code below on gcc and clang compilers, but this code compiles only on trunk branch of gcc and clang
#include <atomic>
#include <thread>
int main()
{
std::atomic_flag f;
std::thread t([&]
{
f.wait(false);
std::cout << "gggg" << std::endl;
});
using namespace std::chrono_literals;
std::this_thread::sleep_for(3s);
f.test_and_set();
f.notify_all();
t.join();
std::cout << "Hello World!\n";
}
Does anyone know about implementation status of this features?

This kind of latest feature heavily depends on the compiler.
Use boost::atomic_flag. it is original and compiler/os independent

Related

std::ranges::remove still not suported / broken on clang trunk when using libstdc++?

Works fine on gcc trunk, but not on clang trunk, both with libstd++.
Or am I missing something exceedingly obvious?
Godbolt
#include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
std::ostream& operator<<(std::ostream& os, const std::vector<int>& v) {
for (auto&& e: v) os << e << " ";
return os;
}
int main() {
auto ints = std::vector<int>{1,2,3,4,5};
std::cout << ints << "\n";
auto [first, last] = std::ranges::remove(ints, 3);
ints.erase(first, last);
std::cout << ints << "\n";
}
gcc is clean. clang gives a WALL OF ERRORS, complaining about missing "__begin".
UPDATE: If I use -stdlib=libc++ then clang says "never heard of it", so I guess they are just not there yet.
new Godbolt
This seems to be a Clang bug, affecting ranges when using libstdc++, see this issue with the underlying cause which is still open and other issues linked to it as duplicates with examples how it affects ranges with libstdc++. There seems to have been some work on it about two weeks ago.
In libc++ std::ranges::remove does not seem to be implemented yet as you noticed and as stated on its status page for ranges implementation.

How to compile generator and coroutine using c++2a on Mac

I am setting up my MacBook for C++20 and having problem to compile the code. I have installed latest Xcode, llvm and gcc. Here is the code that I am trying to compile
#include <chrono>
#include <experimental/coroutine>
#include <future>
#include <iostream>
using namespace std;
generator<int> getInts(int first, int last) {
for (auto i = first; i <= last; ++i) {
co_yield i;
}
}
int main() {
for (auto i : getInts(5, 10)) {
std::cout << i << " ";
}
}
however I am getting following error:
$ g++ gen.cpp -std=c++2a
In file included from gen.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/experimental/coroutine:66:5: warning:
<experimental/coroutine> cannot be used with this compiler [-W#warnings]
# warning <experimental/coroutine> cannot be used with this compiler
^
appreciate any insight how to resolve this compile issue.
The Coroutines TS has not been voted into C++20 (indeed, there have been three separate votes, with approval not achieving consensus in all of them), so even if GCC implemented it (which it doesn't), it wouldn't be activated by the -std=c++2a switch.
Now, Clang does implement the Coroutines TS, which you have to turn on with -fcoroutines-ts. And apparently, you have to be using libc++.

Code compiles on GCC 6.2.0 built by MinGW-W64 when it shouldn't

This sample code should not be valid in c++:
#include <iostream>
#include <type_traits>
class Test
{
public:
void func();
};
int main()
{
std::cout << std::is_same<decltype(Test::func), //error, use &Test::func
void(Test::*)(void)>::value << std::endl;
}
But compiles in gcc (x86_64-posix-seh-rev1, Built by MinGW-W64 Project) 6.2.0,
and outputs true when using whichever Test::func or &Test::func.
Yet it rightfully fails to compile with any other compiler, or even with GCC 6.2.0 here.
What's wrong with MinGW-W64? MinGW-W64 GCC 5.3.0 also compiles this.

problems with C++11-library and g++ 4.4.7

I am working on a server with GCC version 4.4.7, and I am forced to work with this version unfortunately. I want to make use of the <random> library of C++0x, but I read here that in this version uniform_real_distribution is called uniform_real. When I try to call this function and normal_distribution, I don't get useful output. See this example:
#include <random>
#include <iostream>
using namespace std;
int main()
{
typedef std::mt19937 Engine;
typedef std::uniform_real<double> Dis1;
typedef std::normal_distribution<double> Dis2;
Engine eng(0);
Dis1 dis1(0, 1);
cout << dis1(eng) << endl; //OUTPUTS 3.49921e+09
Dis2 dis2(0, 1);
cout << dis2(eng) << endl; //STALLS, NO OUTPUT
return 0;
}
I compile with g++44 -std=c++0x main.cpp and I have shown what output I get. What is the issue here?
C++11 support in gcc 4.4 is rather sparse.
The release notes for gcc 4.5 include
Improved experimental support for the upcoming ISO C++ standard,
C++0x
specifically mentioning <random>.

Using regex_search from the C++ regex library

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.