Is this a Bug of VS 2005?

L

Lighter

Is this a Bug of VS 2005?

We know size_t is not a built-in data type in C++, rather, it is just a
typedef declaration.

However, I found the following code could be normally compiled with VS
2005:

// Note that I don't include any header file in this source file

size_t Test(size_t a)
{
return a;
}

int main()
{
size_t n = Test(8);

return n;
}

Why? Is this really a bug of VS 2005?
 
J

Jack Klein

Is this a Bug of VS 2005?
No.

We know size_t is not a built-in data type in C++, rather, it is just a
typedef declaration.

However, I found the following code could be normally compiled with VS
2005:

// Note that I don't include any header file in this source file

size_t Test(size_t a)
{
return a;
}

int main()
{
size_t n = Test(8);

return n;
}

Why? Is this really a bug of VS 2005?

It can be considered an extension, and extensions are perfectly valid
as long as they do not affect the behavior of a strictly conforming
program. And this can't, because the program has undefined behavior
by using an undefined type.

Furthermore, an attempt to cause a conflict by adding something like:

typedef double size_t;

....in your source file causes even more undefined behavior because all
typedef's inherited from the C library, which includes size_t, are
reserved for the compiler in both the global and std namespaces.
 
L

Lighter

to Jack Klein:

Thank you for your answer. However, I don't think this is an extension
for C++ made by VS 2005. The reason for this is that VS 2005 can still
correctly compile the code above-mentioned even I set the compiler
option /Za.
 
D

Darwin Lalo

Lighter 写é“:
Is this a Bug of VS 2005?

We know size_t is not a built-in data type in C++, rather, it is just a
typedef declaration.

However, I found the following code could be normally compiled with VS
2005:

// Note that I don't include any header file in this source file

size_t Test(size_t a)
{
return a;
}

int main()
{
size_t n = Test(8);

return n;
}

Why? Is this really a bug of VS 2005?



Some "Header Files" is Default Includes.
 
A

Alf P. Steinbach

* Jack Klein:
It can be considered an extension,
Yes.

and extensions are perfectly valid
as long as they do not affect the behavior of a strictly conforming
program.

Yes, but a diagnostic must be issued (in standards-compliant mode). The
compiler can then go on to compile the rest with no further diagnostics.

I think it's a bug, possibly one intentionally introduced to make some
code that relies on this behavior, compile cleanly.
 
R

Ron Natalie

Jack said:
It can be considered an extension, and extensions are perfectly valid
as long as they do not affect the behavior of a strictly conforming
program. And this can't, because the program has undefined behavior
by using an undefined type.

Except the compiler should say something. The program is ill-formed.
 
B

Bo Persson

"Darwin Lalo" <[email protected]> skrev i meddelandet

Lighter ??:
Some "Header Files" is Default Includes.

Not really, but size_t is the type of the return value of the sizeof
operator. That can be used without including any headers, so the
compiler must have builtin knowledge of size_t. This is just made
visible here.


Bo Persson
 
R

Ron Natalie

Bo said:
Not really, but size_t is the type of the return value of the sizeof
operator. That can be used without including any headers, so the
compiler must have builtin knowledge of size_t. This is just made
visible here.
It does not need to be visible. The sizeof operator is quite
capable of working without the NAME size_t being defined. It
does on just about any other compiler OTHER than visual studio.
 
J

Jack Klein

Lighter ???




Some "Header Files" is Default Includes.

That was my first thought, but did you see the note the OP put above
the function? C++ specifically allows any standard header to include
any or all other standard headers. But it certainly does not give the
implementation permission to include any standard headers in a
translation unit that includes nothing.
 
J

Jack Klein

* Jack Klein:

Yes, but a diagnostic must be issued (in standards-compliant mode). The
compiler can then go on to compile the rest with no further diagnostics.

I think it's a bug, possibly one intentionally introduced to make some
code that relies on this behavior, compile cleanly.

The OP said it "compiled normally", which to me means it produced an
executable. He did not specify whether or not it issued a diagnostic.
 
J

Jack Klein

Except the compiler should say something. The program is ill-formed.

The OP said it "compiled normally", which to me means it produced an
executable. He did not specify whether or not it issued a diagnostic.
 
L

Lighter

Jack said:
The OP said it "compiled normally", which to me means it produced an
executable. He did not specify whether or not it issued a diagnostic.

I'm the OP. I meant that VS 2005 did not give any error or warning
message during it compiled the code mentioned above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top