GCC 4.2.2 unsigned short error while casting
this line of code does not compile for me in GCC 4.2.2
m_Pout->m_R[i][j] = MIN(MAX(unsigned short(m_Pin->m_R[i][j]), 0), ((1 << 15) - 1));
error: expected primary expression before 'unsigned
however if I add curly braces in (unsigned short)
it works fine.
Could you please explain what type of casting (distribution) is being done here?
Why can't the lexical parser / compiler understand this C ++ code in GCC?
Can you suggest a "better" way to write this code? GCC 4.2.2 support (no C ++ 11 and cross platform)
source to share
I think the answer to the question about Bathsheba is at least misleading. short(m_Pin->m_R[i][j])
- this is the cast. Why are these unnecessary things unsigned
? This is because it is unsigned short
not a simple type specifier. The cast syntax T(E)
only works if T is the only token and there unsigned short
are two tokens.
Other types that are written with more than one token are char*
and int const
, and therefore are also invalid casts: char*(0)
and int const(0)
.
C static_cast<>
are < >
balanced, so a type can be named with a sequence of identifiers, evenstatic_cast<int const*const>(0)
source to share
You can use ยง2 in Bathsheba's answer , but more idiomatic use static_cast
in C ++:
static_cast<unsigned short>(m_Pin->m_R[i][j])
By the way, your error is not related to GCC. You will get the same if you use Clang / LLVM or any (C ++ 99 or C ++ 11) standard C ++ compiler.
But regardless, you should be using a much newer version of GCC. In July 2015, the current version of GCC is 5.1 and your GCC is 4.2. 2 version from 2007, which is very ancient.
Using a more recent version of GCC is worth it because:
-
it allows you to stick with a newer version of C ++ for example. C ++ 11 (compile with
-std=c++11
or-std=gnu++11
) -
the latest GCCs have improved their diagnostics. Compiling with help
-Wall -Wextra
will help a lot. -
the latest GCCs are better optimized and you will get more performance from your code
-
Recently, GCC has a more standard C ++ standard library
-
the latest GCC are better suited for debugging (with the recent the GDB) and have disinfectant parameters (
-fsanitize=address
,-fsanitize=undefined
and other parameters-fsanitize=
....), which helps to find errors -
recent GCCs are more standard compliant
-
latest GCCs are configurable via plugins including MELT
-
the older GCC 4.2 is no longer supported by the FSF and you will have to pay a lot of money to a few companies that support it.
You do not need root access to compile from its source GCC 5 compiler (or cross compiler). Read the installation procedures . You will build GCC with your specific libc in mind (and you can even use musl-libc if you like ....), perhaps by compiling outside of the source tree after setting up with a command like
...your-path-to/gcc-5/configure --prefix=$HOME/soft/ --program-suffix=-mine
then make
, then make install
, then add $HOME/soft/bin/
to yours PATH
and use gcc-mine
andg++-mine
source to share