Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "Expected '(' for function-style cast or type construction" error mean?

Tags:

c++

I'm getting the error "Expected '(' for function-style cast or type construction", and have done my best to research the meaning of this error online, but have been unable to find any documentation of what causes this error.

All the related questions on Stack Overflow that I have found bug fix a specific code snippet and do not explain more generally what is causing the error.

These include

  1. Expected '(' for function-style cast or type construction answer highlights several issues with the code. Which issue is actually causing the error is unclear.
  2. c++ Xcode expected '(' for function-style cast or type construction defines functions inside a main function. This seems like a clear syntax problem, but why this specific error is produced is still unclear to me.
  3. '(' for function-style cast or construction type Xcode error. In this last example, the OP calls a function in a way that looks very similar to a function declaration, plus they declare a function with the same name but a different signature. Based on where the error is thrown and the message from the error, it seems that the error has something to do with function declarations.

Can I get a documentation-style answer that translates what "function-style cast" and "type construction" mean in simple english? When does the compiler choose to throw this error instead of some other error?

I don't want an answer that is specific to my own error, but as requested, here is my MCVE

#include <boost/integer_traits.hpp>

class Test{
    const double MAX_DEPTH_VAL = (double) boost::integer_traits<unsigned short>.const_max;
    const double MIN_DEPTH_VAL = (double) boost::integer_traits<unsigned short>.const_max;

};

I was led to believe that this syntax was possible, by this answer https://stackoverflow.com/a/2738576/3303546

like image 397
Cecilia Avatar asked Oct 15 '16 19:10

Cecilia


2 Answers

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.

like image 119
M.M Avatar answered Nov 09 '22 23:11

M.M


I fixed this error by adding -std=c++20 in my Makefile.

like image 21
Antonin Avatar answered Nov 10 '22 00:11

Antonin