Erroneous error C2099: initializer is not a constant - by A. Sinan Unur

Status : 

 


5
0
Sign in
to vote
ID 1304939 Comments
Status Active Workarounds
Type Bug Repros 0
Opened 5/5/2015 4:29:03 AM
Access Restriction Public

Description

Consider the following simple C program:

#include <stddef.h>
#include <stdio.h>

struct x {
    int y;
    char z;
};

static const size_t i = +offsetof(struct x, z);

int main(void) {
    printf("MSCVER: %d\n", _MSC_VER);
    return 0;
}

Compiling with VS2013CE command line tools:

C:\...\Temp> cl t.c
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

t.c
Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:t.exe
t.obj

C:\...\Temp> t.exe
MSCVER: 1800

Now, try the VS2015RC-CE tools:

C:\...\Temp> cl t.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.22816 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

t.c
t.c(9): error C2099: initializer is not a constant

---

Now, consider a simple change:

static const size_t i = 42+offsetof(struct x, z);

This causes the VS2015RC-CE tools to compile without problems:
C:\...\Temp> cl t.c                             
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.22816 for x64   
Copyright (C) Microsoft Corporation.  All rights reserved.            
                                                                      
t.c                                                                   
Microsoft (R) Incremental Linker Version 14.00.22816.0                
Copyright (C) Microsoft Corporation.  All rights reserved.            
                                                                      
/out:t.exe                                                            
t.obj                                                                 
                                                                      
C:\...\Temp> t.exe                              
MSCVER: 1900

Clearly,

+offsetof(struct x, z);

is equivalent to 0+offsetof(struct x, z), and, as such is just as constant an expression as 42+offsetof(struct x, z) and should compile as such.

I encountered this bug while trying to build Perl using VS2015RC tools. It prevents sv.c from compiling.

Thank you for making the Community Editions of Visual Studio tools available, and thank you for your consideration of this bug report.

-- A. Sinan Unur 

Sign in to post a comment.
Posted by Microsoft on 5/28/2015 at 5:20 PM
Hi A. Sinan Unur,

This bug has been fixed and the fix will be included in a future release of Visual Studio.

Karl Niu (kaniu@microsoft.com)
Visual C++ Team
Posted by Microsoft on 5/5/2015 at 6:03 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If you require immediate assistance with this issue, please contact product support at http://support.microsoft.com/ph/1117.