I don't see anything wrong per-se with naked brackets. They're a part of the language, and they're well defined. Historically, one place I have found them useful is when working with code that uses status codes instead of exceptions, while keeping const goodness:
const StatusCode statusCode = DoThing();
if (statusCode == STATUS_SUCCESS)
Foo();
else
Bar();
const StatusCode statusCode2 = DoAnotherThing(); // Eww variable name.
...
The alternative would be:
{
const StatusCode statusCode = DoThing();
if (statusCode == STATUS_SUCCESS)
Foo();
else
Bar();
}
{
// Same variable name, used for same purpose, easy to
// find/replace, and has const guarantees. Great success.
const StatusCode statusCode = DoAnotherThing();
...
}
The same applies to objects such as thread lockers that use RAII as well (Mutex objects, semaphores, etc), or generally any kind of resource that you may want to have an extremely short lifetime (file handles, for example).
Personally, I think the reason it's rare is that it can be indicative of a code smell (although not always). Where there are naked brackets, there may be an opportunity to factor out a function.
To take your example, if there is more than one job for swap_int
, then the function is doing more than one thing. By extracting the actual swap code into another function, you can encourage reuse! For example:
template <typename T>
void swap_anything(T &first, T& second)
{
T temp = first;
first = second;
second = temp;
}
// -------------------------------------------
void swap_int(int& first, int& second)
{
// Do stuff...
swap_anything(first, second);
// Do other stuff...
}