Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7944 Discussions

C compiler: confusing diagnostics "error: function call is not allowed in a constant expression"

pmor
Beginner
4,066 Views

This code (case 1):

#define EXPR  int(*)[f()]
int f(void)
{
        return sizeof(EXPR);
}

leads to no diagnostics:

$ icc -std=c11 -pedantic -Wall -Wextra -c
<nothing>

and the following code is generated

f:
        mov       eax, 8                                        #24.9
        ret   

Here we see that the there is no "call f". Hence, we conclude that f() is not evaluated.

This code (case 2):

#define EXPR  int(*)[f()]

int s = sizeof(EXPR);

leads to diagnostics:

error: function call is not allowed in a constant expression

This error looks confusing, because in case 1 we concluded that f() is not evaluated. 

C11, 6.6 Constant expressions, Constraints, 3:
> Constant expressions shall not contain assignment, increment, decrement, function-call,
or comma operators, except when they are contained within a subexpression that is not
evaluated. 115)
> 115) The operand of a sizeof or _Alignof operator is usually not evaluated (6.5.3.4).

Version: icc 2021.5.0 on x86-64 on Linux

0 Kudos
4 Replies
pmor
Beginner
4,054 Views

And for this code:

 

int f(void);
int s = _Alignof( int(*)[ f() ] );

 

produced:

 

error: function call is not allowed in a constant expression

 

looks definitely wrong because:

C11, 6.6 Constant expressions, Constraints, 3:
> Constant expressions shall not contain assignment, increment, decrement, function-call,
or comma operators, except when they are contained within a subexpression that is not
evaluated. 115)

and:

C11, 6.5.3.4 The sizeof and _Alignof operators, Semantics, 3:
> The _Alignof operator yields the alignment requirement of its operand type. The
operand is not evaluated and the result is an integer constant.

0 Kudos
DitiD_Intel
Moderator
4,030 Views

Hi,


Thank you for posting in Intel Communities.



We are working on your issue and we will get back to you soon.



Thanks and Regards,

Ditipriya.






0 Kudos
DitiD_Intel
Moderator
4,021 Views

Hi,

 

I tried compiling the sample codes with various compilers (icc, icx and gcc).

 

For more information, please refer to the attached screenshots.

 

Not sure if you are aware of it, but the C++ Classic compiler will enter "Legacy Product Support" mode signaling the end of regular updates. Please see the below article for more details.

 

https://www.intel.com/content/www/us/en/developer/articles/technical/adoption-of-llvm-complete-icx.h... 

 

We suggest you try with latest icx compiler.

 

Thanks and Regards,

Ditipriya.

 

 

0 Kudos
DitiD_Intel
Moderator
3,874 Views

Hi,


The version against which you reported your issue is no longer targeted for fixes. Please check out the new versions of Intel oneAPI Toolkits for the latest features.


Thanks and Regards,

Ditipriya.


0 Kudos
Reply