VC++ 7.10.2292 treats initialized declaration as a definition 
Author Message
 VC++ 7.10.2292 treats initialized declaration as a definition

I have found another bug in the VC++ 7.1 Final beta compiler.
This time, the demonstration requires 2 cpp files and a header:

---- FILE decl.h ----
class C
{
public:
    // Initialized declaration of C::i.
    static const int i = 4 ;

Quote:
} ;

---- END decl.h ----

---- FILE decl.cpp ----
#include "decl.h"
// Definition of the C::i variable.
const int C::i ;
---- END decl.cpp

---- FILE decl2.cpp ----
#include "decl.h"
int main( void ) { return C::i ; }
---- END decl2.cpp ----

The header file contains a class with a static integral constant.
The constant is declared and initialized in the class.
There is no definition of the constant in the header file.

The decl.cpp file contains a definition of the C::i constant. (The standard,
paragraph 9.4.2.4. requires the definition in some cases.)

The decl2.cpp file includes the header file and defines main().

When both cpp files are compiled, both of them compile without an error.
However, linking state generates an error saying that C::i symbol is
defined more than once. Apparently, MSVC treats the initialized declaration
in the header file as a definition, too - which is wrong according to the
standard.

Michal Rehacek



Mon, 26 Sep 2005 22:16:14 GMT  
 VC++ 7.10.2292 treats initialized declaration as a definition



Quote:
> I have found another bug in the VC++ 7.1 Final beta compiler.
> This time, the demonstration requires 2 cpp files and a header:

> ---- FILE decl.h ----
> class C
> {
> public:
>     // Initialized declaration of C::i.
>     static const int i = 4 ;
> } ;
> ---- END decl.h ----

> ---- FILE decl.cpp ----
> #include "decl.h"
> // Definition of the C::i variable.
> const int C::i ;
> ---- END decl.cpp

> ---- FILE decl2.cpp ----
> #include "decl.h"
> int main( void ) { return C::i ; }
> ---- END decl2.cpp ----

> The header file contains a class with a static integral constant.
> The constant is declared and initialized in the class.
> There is no definition of the constant in the header file.

> The decl.cpp file contains a definition of the C::i constant. (The
standard,
> paragraph 9.4.2.4. requires the definition in some cases.)

Yes, but exactly which cases is somewhat changed in the upcoming Technical
Corrigendum. See

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#48

Quote:
> The decl2.cpp file includes the header file and defines main().

> When both cpp files are compiled, both of them compile without an error.
> However, linking state generates an error saying that C::i symbol is
> defined more than once. Apparently, MSVC treats the initialized
declaration
> in the header file as a definition, too - which is wrong according to the
> standard.

> Michal Rehacek

Bo Persson



Tue, 27 Sep 2005 01:04:31 GMT  
 VC++ 7.10.2292 treats initialized declaration as a definition

Quote:
> Yes, but exactly which cases is somewhat changed in the upcoming Technical
> Corrigendum. See

> http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#48

Yes. However, MSVC should at least allow the definition, which it does not.
It fails to compile code that is right according to the standard.
(The definition is sometimes required - and it is not important when -,
the important thing is that it is allowed to be present in all other cases.)

Thanks for pointing me to the nice document however :)

Michal Rehacek



Tue, 27 Sep 2005 21:46:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q How to initialize CStringArray in class definition

2. Compiler error C2244 (unable to match definition to declaration) - template class function

3. Function declaration/definition match bug

4. Declaration or Definition?

5. definition or declaration?

6. extern declaration and definition within the same translation unit

7. differences in function DECLARATION and DEFINITION

8. Function Declaration/Definition Mix

9. Tentative definitions, redundant declarations.

10. declaration between function header and function definition ???

11. what is a different between the variable DEFINITION and variable Declaration

12. declarations and type definitions

 

 
Powered by phpBB® Forum Software