The Lone C++ Coder's Blog from The Lone C++ Coder's Blog
This is a repost from my old blog - I’m moving some of my older articles over as nobody knows how long the machine that hosts that blog will still be around. highlight-changes-mode – as the name implies, it highlights changes that you make to a file. I do find it useful for the typical scenario of checking out a file, making a couple of smaller changes to it and then having to diff it to work out what you actually changed.If we consider software creation a craft, Is it time to ‘bring our own tools’?
The Lone C++ Coder's Blog from The Lone C++ Coder's Blog
If you look at really productive programmers - like the top 10-20% - there are usually a couple of characteristics that they share. Aptitude and in-depth understanding of both the system they are working on and the technologies involved is obviously one very important factor. Another factor that tends to be overlooked is that these programmers are also masters of their tools in the same way that a master craftsman - say, a carpenter - is also a master of their tools.Effective C++11/14
Frances Buontempo from BuontempoConsulting
Work sent me on Scott Meyers' latest course at Developer Focus. He gave us these guidelines:- Prefer auto to explicit type declarations - remember that auto + {expr} => std::initializer_list
- Prefer nullptr to 0 and Null
- Prefer scoped enums to unscoped enums
- Distinguish () and {} when creating objects - the latter disallows narrowing, that might be good
- Declare functions noexcept whenever possible - esp. swap move
- Make const member functions threadsafe - make them bitwise const or internally synchronised
- Distinguish "universal references" from r-value references - "universal references" is his phrase, just note if you see type&&& type might not be an r-value
- Avoid overloading on && - typically r-value ref versus l-value ref is ok, but just r-value ref might be trouble cos it's greedy
- Pass and return r-value refs via std::move, universal refs by std::forward - and allow RVO to happens as before in C++98/03
- Understand reference collapsing See SO
- Assume that move operations are not present, not cheap and not used
- Make std::thread unjoinable on all paths - even if there's an exception
- Use std::launch::async with std::async if asynchronicity is essential - but is it really essential?
- Be aware of varying thread handle destructive behaviour
- Create tasks not threads
- Consider void functions for one-shot event communication
- Pass parameterless functions to std::async, atd::thread}} and {{{std::call_once - the arguments are unconditionally copied, as with std::bind. Use a lambda instead
- Use native handles to transcend the C++11/14 API - if you need to configure your thread, but don't use a thread, so you won't need too
- Prefer lambdas to std::bind - inlining is possible
- Beware default captures in member functions - [=] captures the this pointer, and so member variables via this->variable, which could dangle and are "by ref" i.e. will match [&]. C++14 will add stuff to help
- Use std::make_shared and std::make_unique whenever possible
- Keep abreast of standardisation
Speaking: Running Effective Rehearsals
Pete Goodliffe from Pete Goodliffe
I'll be speaking at The Worship Collective conference in Cambridge, UK on June 29th. This is an awesome event for musicians and worship leaders.I'm leading a seminar entitled Running Effective Rehearsals. Obviously, this is a really practical subject, but I promise it'll be fun too. Hopefully there will be some practical wisdom to apply, and some encouraging advice to take away.
How to enable (hack) git-p4 in msysgit for Windows
The Lone C++ Coder's Blog from The Lone C++ Coder's Blog
The default installation of msysgit (aka the official git client for Windows) is unfortunately built without python support. There are understandable reasons as to why this is, starting with “where the heck do I find the various python versions on Windows”. For me the problem was that I needed git-p4 to extract some code history out of a Perforce repository and guess what, git-p4 is written in Python. Only solution for me was that I had to find a way to make this work short of throwing Linux in a VM just to get a git import going.Windows 8 has the telnet client turned off by default?
The Lone C++ Coder's Blog from The Lone C++ Coder's Blog
For security reasons, apparently. I can see that making sense with the telnet server but the client? It’s an invaluable network debugging tool, after all, especially in heterogeneous networks. Anyway, here is how you re-enable it. Just leave the server off.I guess that’s one way of mounting an SSD
The Lone C++ Coder's Blog from The Lone C++ Coder's Blog
The perils of buying a used computer - yes, I am too cheap or just not rich enough to buy a new Mac Pro - is that sometimes you find that you inherited “interesting” fixes. Like this SSD mount: Yes, that’s electrical tape and no, I don’t agree with this special mounting method. At least they did put some electrical tape between the case of the SSD and the case of the DVD drive.Hello concurrent world
Frances Buontempo from BuontempoConsulting
From Anthony William's "C++ concurrency in action - practical multithreading", section 1.4.1 gives a simple "Hello world" program using C++11's thread.
#include <iostream>
#include <thread>
void hello()
{
std::cout <<"Hello concurrent world!\n";
}
int main()
{
std::thread t(hello);
t.join();
}
After Matthew Wilson re-starting his series in Overload, "Quality Matters #7 Exceptions: the story so far" http://accu.org/var/ uploads/journals/Overload114. pdf page 10ff, I had a nagging feeling I should put some exception handling round this.
First question, what happens if we make the hello throw an exception? For example, what would this do?
std::thread t_trouble( []{ throw std::exception("Oops");} );
It calls abort. The thread function mustn't let exceptions escape. Also, main should probably catch some exceptions; for example, maybe there aren't enough resources to start the thread yet.
#include <iostream>
#include <thread>
void hello()
{
try
{
std::cout <<"Hello concurrent world!\n";
}
catch(const std::exception& e)
{
//erm... what to do with it?
}
}
int main()
{
try
{
std::thread t(hello);//can I pass parameters? Nico says I can to async (page 964)
t.join(); //Nico says we can do a t.detach and when main exits it will get stopped
}
catch(const std::system_error& e) //pulled in by thread I presume
{
if(e.code() == std::errc::resource_ unavailable_try_again)
{
std::cout << "Try again\n";
}
}
catch(const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
Right, so now we are ignoring any exceptions that get thrown.
What should I do with any exceptions I get in a function that's sent to a thread? I could use std::exception_ptr, and std::rethrow_exception when a client tries to get the result. It might be better if I read all of Anthony's book (esp Chapter 8) and use std::packaged_task instead.
accu-general (http://accu.org/index.php/mailinglists) helpfully told me to read all the chapters in the book concurrently.
Writing: Bug Hunting
Pete Goodliffe from Pete Goodliffe
The latest C Vu magazine from ACCU is out now. It contains my latest Becoming a Better Programer column. This month it's called Bug Hunting and, as you might guess, is about the art of debugging code.This was inspired by conversations with Greg Law in the lead up to the 2013 ACCU conference.
About a month without Google Reader
The Lone C++ Coder's Blog from The Lone C++ Coder's Blog
As a bit of an RSS junkie - see previous post - I had to go look for alternatives to Google Reader. I’ve been a feedly user on and off for a few years but I was never that taken with it. It does seem to do mostly do what it says on the tin and having various tablet apps available for feedly is a good thing, but it tends to run into a few issues with high-volume feeds (craigslist feeds, I’m looking at you).