What does the “Expected ‘(‘ for function-style cast or type construction” error mean?

This is a syntax error. Now, non-programmers or amateurs might hear the term syntax error and equate it with a general bug. But in C++ (and other languages) it has a more specific meaning.

There is a language grammar which is a set of rules by which the compiler, at an early stage of translation, breaks up the source code into logical groups. This is before any meaning is ascribed to those groups (that part is sometimes called semantic checking).

The syntax error you saw means that the compiler could not match up the source code to the grammar rules for C++. Precisely because it could not do this — it’s hard for the compiler to know what the programmer intended. So, syntax error messages are often guesses or don’t relate to the programmer intention.

When you see this error, the compiler is suggesting a way of changing the code that would possibly match one of the grammar rules, but that may or may not actually be a good fix in the situation.

So, you can treat this sort of error just as “general syntax error”, not worrying too much about the details of the error. To fix it, go back to simpler expressions that you are sure are not syntax errors, and then build up towards what you wanted to write.

An analogy for English language might be the sentence “I the room went of”. Imagine some language translation software. This doesn’t match any known sentence structure but what error message can it report? The actual suggestions probably won’t help you to fix the sentence.

In your specific example, there is a syntax error. The g++ error message is different:

error: expected primary-expression before ‘.’ token

where primary-expression is an entry in the C++ grammar. g++ sees the . token and assumes you mean the member access operator. But the grammar says that the left-hand operand of the member access operator must be a primary-expression (and the semantic rules say that this primary-expression denotes the object whose member you want to access).

However in your actual code the left-hand side is (double) boost::integer_traits<unsigned short> which does not match the grammar specification for primary-expression. (In fact it’s a type name). The compiler can’t proceed any further from here so it bails out.

Your compiler also failed to match the code to any grammar rule, but it guessed you were trying to write a function-style cast or type construction.

“Function-style cast” means code like int(5.0), so perhaps it recognized boost::integer_traits<unsigned short> as a type-name, and it guessed that you meant boost::integer_traits<unsigned short>(const_max), i.e. casting some variable const_max to that type.

I’m not sure what your compiler means by “type construction” here.

NB. If you want to know how to fix the actual code in your question, I’d suggest starting a new question where you post the code and error message and ask how to fix the code.

Leave a Comment