Menu

#18 Questionable language construct (in base/PdfString.cpp)

SVN TRUNK
closed
nobody
None
2020-05-26
2018-04-09
No

In method PdfString::setFromWchar_t this code construct is questionable:

if( sizeof(wchar_t) == 2 )
{
    ...
}
else
{
    ...
}

It triggers warnings in some compilers and can raise an error on others when compiling with some comformance settings. This is because, for any chosen compilation settings, the expression is constant (either true or false) and leads to dead code either in the if-branch or the else-branch.

It could be made valid by using a constexpr as in:

constexpr bool have_utf16 = (sizeof(wchar_t) == 2);
if( have_utf16 )

because the intent of the programmer becomes clear and can't be mistaken for a possible... mistake.

Albeit for portability with C++11, it would probably be better to use some preprocessing statement to distinguish between cases where sizeof(wchar_t) would be 2 or different.

Discussion

  • zyx

    zyx - 2020-05-22

    I'm not able to reproduce this myself, not sure what warning the compiler shoul dhave enabled, but I took your suggested change and comitted it as r2009:
    https://sourceforge.net/p/podofo/code/2009

     
  • zyx

    zyx - 2020-05-22
    • status: open --> closed
     
  • zyx

    zyx - 2020-05-26

    It turned out that the 'constexpr' forces C++11 compilation, thus I made the change to use 'const' instead (which may or may not work similarly). Done in r2010:
    https://sourceforge.net/p/podofo/code/2010

     
MongoDB Logo MongoDB