I've recently had to implement the common pattern of reading from a file or, given a filename of "-", from stdin. There is an abundance of solutions on SO, but I found mine to be shorter.
string from (some arg);
ifstream fromfile;
istream& read = (from == "-")
? cin
: (fromfile.open(from), fromfile);
Key part is moving fromfile.open
to a branch. This lets us use a reference instead of a pointer while avoiding checking for equality with "-" twice, which happens in most other solutions.
However, I did not find this kind of approach used anywhere else. I presume that is mainly because it is somewhat harder to read. Or is there something else?