News for hackers. Stuff that matters.
Re:C++17 Filesystem – Writing a simple file watcher by zeroname on Monday January 14, 2019 @04:43PM (#18906820)
Guys, don't do this. There are OS-specific APIs to do this stuff efficiently. No, that's not portable. No, those APIs aren't "nice to use". But it's the "right" way to do it.

When you do that kind of stuff, keep in mind that many programs modify files by writing to a temp file and then renaming that file.

On a secondary note, "modern C++" needs to be stopped. This is absurd:

std::chrono::duration<int, std::milli>

Just say no. Don't include stuff like <chrono> or <functional>. Ignore Bjarne's opinions. Don't read books by Scott Meyers. Being effective with C++ means understanding its "powers" and then not using them.

Re:C++17 Filesystem – Writing a simple file watcher by asveikau on Monday January 14, 2019 @05:11PM (#18907022)
I agree and came here to comment that you should not use this code.

The code has sleep calls and polls the filesystem. The nonportable OS support will actually block and have the kernel wake you up when there are actual changes - and not wake you when there is no change - much kinder on the CPU!

Just say no indeed, to polling the filesystem with sleep calls. (Maybe as a last resort if the OS specific support is unavailable?)

Re:C++17 Filesystem – Writing a simple file watcher by stennisrl on Tuesday January 15, 2019 @01:17AM (#18909159)
While I'll agree that this particular filesystem monitoring class is pretty rough, I think it's a bit of a knee-jerk reaction to just outright dismiss things like <chrono> or <functional>. There are definitely situations where a very dialed-in API is preferred over the standard offerings, but it all depends on your use case.
Re:C++17 Filesystem – Writing a simple file watcher by Topgamer7 on Monday January 14, 2019 @03:54PM (#18906367)
Those files have so many "std::" references. Would this not be a good case to utilize "using namespace std"?
Re:C++17 Filesystem – Writing a simple file watcher by favorited on Monday January 14, 2019 @04:11PM (#18906514)
It's a header-only class[0], and "using namespace" in headers is discouraged.

[0] https://github.com/sol-prog/cpp17-filewatcher/blob/master/Fi...

Re:C++17 Filesystem – Writing a simple file watcher by Someone on Monday January 14, 2019 @05:46PM (#18907276)
But one could use it scoped to the FileWatcher class or to each member function, or is my C++ too rusty?
Re:C++17 Filesystem – Writing a simple file watcher by jhalstead on Monday January 14, 2019 @11:55PM (#18908941)
I think it's too rusty, unless I'm misunderstanding https://abseil.io/tips/153. It says,

The vast majority of C++ users think that the using-directive is injecting names into the scope where it’s declared. ... In reality, the names are injected into the nearest common ancestor of the target namespace (::testing) and the usage namespace (::totw::example::anomymous). In our example, that’s the global namespace!

Re:C++17 Filesystem – Writing a simple file watcher by favorited on Monday January 14, 2019 @07:14PM (#18907838)
IIRC it can be used from namespace or function scope, but not class.
Re:C++17 Filesystem – Writing a simple file watcher by wmu on Monday January 14, 2019 @03:25PM (#18906130)
std::unordered_map has method count(), C++20 will be probably just a wrapper for the method.

https://en.cppreference.com/w/cpp/container/unordered_map/co...

Re:C++17 Filesystem – Writing a simple file watcher by mmisu on Monday January 14, 2019 @03:35PM (#18906210)
Are you confusing .count with .contains ?
Re:C++17 Filesystem – Writing a simple file watcher by wmu on Monday January 14, 2019 @03:53PM (#18906361)
I'm saying that map.contains(x) == bool(map.count(x))
Re:C++17 Filesystem – Writing a simple file watcher by mmisu on Monday January 14, 2019 @04:04PM (#18906448)
+1 I see what you mean now.
Re:C++17 Filesystem – Writing a simple file watcher by wmu on Monday January 14, 2019 @04:47PM (#18906847)
Great! :) My initial comment wasn't clear, sorry for that.
Re:C++17 Filesystem – Writing a simple file watcher by ohiovr on Monday January 14, 2019 @06:11PM (#18907456)
watch ls