Skip to content

Dubious use of brace-init for OpenFOAM lists

In various places there are these types of constructs:

const List<label>& count = {}

This is OK since it will create a zero-sized labelList. However, in general it would be preferable to write as one of these:

const List<label>& count = List<label>();
const List<label>& count = List<label>::null();
  • For the first one, the null constructor (in OpenFOAM) is constexpr noexcept, so the compiler should just rip it out at compile time.
  • For the second one, in OpenFOAM, the static null() method will cast back from a tuple of zeros (ie, nullptr, len = 0) and the compiler will skip over at execution time.

Neither of these are showstoppers. However, the following code really worries me:

adiosStreamPtr->transfer
(
   fde.id(),
   {nCmpts*nGlobalElems},
   {nCmpts*elemOffset},
   {nCmpts*nElems},
   reinterpret_cast<const scalar*>(fde.uList().cdata())
);

In OpenFOAM (probably not foam-extend), the List constructor accepts a std::initializer_list, which is what will be taken. So the array shape is now a labelList with size nCmpts*nGlobalElems of uninitialized values.

Could/should write as

labelList(1, nCmpts*nGlobalElems)

Since we've kicked out the construct from iterator pairs, there should be no ambiguities. However, can also be a bit more certain and use the size/dispatch tag:

labelList(Foam::one{}, nCmpts*nGlobalElems)
Edited by Mark OLESEN
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information