For Programmers: Free Programming Magazines  


Home > Archive > VC STL > February 2005 > VS 6.0 compiler strange error









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author VS 6.0 compiler strange error
Hovhannes Asatryan

2005-02-14, 9:11 pm

Hi guys.
I have written the following code.

#include "stdafx.h"
#include <iostream.h>
#include <string>

using std::string;

int main(int argc, char* argv[])
{
string s("AAA");

printf("%s", s.c_str);

return 0;
}

!!!!! printf("%s", s.c_str); !!!!
This code compiles without any warnings/error on VS 6.0 SP 5 compiler.
Is this valid use of member c_str ?

Is it known problem?


Ulrich Eckhardt

2005-02-14, 9:11 pm

Hovhannes Asatryan wrote:
> #include <iostream.h>


Huh?

> string s("AAA");
>
> printf("%s", s.c_str);

[...]
> !!!!! printf("%s", s.c_str); !!!!
> This code compiles without any warnings/error on VS 6.0 SP 5 compiler.
> Is this valid use of member c_str ?


No, 'c_str' is not a member-object but a memberfunction.

> Is it known problem?


Yes, the fact that it compiles is just another example variadic functions
undermine the C++ typesystem (and typechecking!) and a reason why they
should be avoided.

Uli

Pete Becker

2005-02-14, 9:11 pm

Ulrich Eckhardt wrote:
> Hovhannes Asatryan wrote:
>
>
>
> Huh?
>
>
>
> [...]
>
>
>
> No, 'c_str' is not a member-object but a memberfunction.
>
>
>
>
> Yes, the fact that it compiles is just another example variadic functions
> undermine the C++ typesystem (and typechecking!) and a reason why they
> should be avoided.
>


So when you use variadic functions you're allowed to create arguments
with otherwise illegal code? <g> The fundamental problem here is that
s.c_str isn't legal, but VC++ allows it. It creates a pointer to member
function.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Tom Widmer

2005-02-14, 9:11 pm

Ulrich Eckhardt wrote:
> Hovhannes Asatryan wrote:
>
>
>
> Huh?
>
>
>
> [...]
>
>
>
> No, 'c_str' is not a member-object but a memberfunction.
>
>
>
>
> Yes, the fact that it compiles is just another example variadic functions
> undermine the C++ typesystem (and typechecking!) and a reason why they
> should be avoided.


It's also a compiler bug though - "s.c_str" is not a valid expression
("&string::c_str" is of course), so the code should be rejected
regardless of the use of a variadic function.

Tom
Doug Harrison [MVP]

2005-02-14, 9:11 pm

Pete Becker wrote:

>So when you use variadic functions you're allowed to create arguments
>with otherwise illegal code? <g> The fundamental problem here is that
>s.c_str isn't legal, but VC++ allows it. It creates a pointer to member
>function.


Note also that the following compiles, and IME at least is an even more
likely bug:

printf("%s", s);

This is undefined, and I keep hoping it will be diagnosed one day, but it's
semi-blessed for CString...

--
Doug Harrison
Microsoft MVP - Visual C++
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com