Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
Announcements
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.
7764 Discussions

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

pmor
Beginner
1,684 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
1,672 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.

DitiD_Intel
Moderator
1,648 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.






DitiD_Intel
Moderator
1,639 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.

 

 

DitiD_Intel
Moderator
1,492 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.


Reply