namespace std and standard headers 
October 11th, 2005, 12:05 PM
| | | namespace std and standard headers
If I include both of these headers in a c++ source file:
#include <stdio.h>
#include <cstdio>
would I then be able to do both of:
std::printf("Hello, world!\n");
printf("Hello, world!\n");
--
Simon Elliott http://www.ctsn.co.uk | 
October 11th, 2005, 12:15 PM
| | | Re: namespace std and standard headers
Simon Elliott wrote:[color=blue]
> If I include both of these headers in a c++ source file:
>
> #include <stdio.h>
> #include <cstdio>
>
> would I then be able to do both of:
>
> std::printf("Hello, world!\n");
> printf("Hello, world!\n");
>
> --
> Simon Elliott http://www.ctsn.co.uk[/color]
It seems that you can do both but this is not a recommended as a good
programming practice because you can get into obscure errors or worse
you can mix them and compile but execute erroneous.
If you know what you are doing you may save yourself from some hard
work in C(sometimes)! | 
October 11th, 2005, 05:35 PM
| | | Re: namespace std and standard headers
On 11/10/2005, remus.dragos@gmail.com wrote:
[color=blue]
> It seems that you can do both but this is not a recommended as a good
> programming practice because you can get into obscure errors or worse
> you can mix them and compile but execute erroneous.
> If you know what you are doing you may save yourself from some hard
> work in C(sometimes)![/color]
I'm trying to get some legacy code to compile. I wouldn't write new
code like this!
--
Simon Elliott http://www.ctsn.co.uk | 
October 11th, 2005, 05:45 PM
| | | Re: namespace std and standard headers
On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:
[color=blue]
> If I include both of these headers in a c++ source file:
>
> #include <stdio.h>
> #include <cstdio>
>
> would I then be able to do both of:
>
> std::printf("Hello, world!\n");
> printf("Hello, world!\n");[/color]
Keep in mind that these will be separate streams, flushed at different
times. So your output may not come out sequentially as you would expect
without explicit stream flushing.
- Jay | 
October 11th, 2005, 07:45 PM
| | | Re: namespace std and standard headers
In article <434ba910$0$38039$bed64819@news.gradwell.net>,
"Simon Elliott" <Simon at ctsn.co.uk> wrote:
[color=blue]
> If I include both of these headers in a c++ source file:
>
> #include <stdio.h>
> #include <cstdio>
>
> would I then be able to do both of:
>
> std::printf("Hello, world!\n");
> printf("Hello, world!\n");[/color]
Yes, this should work. Those two print statements should also work if
you only #include <stdio.h>, but your odds are better of it working with
both includes.
-Howard | 
October 11th, 2005, 07:55 PM
| | | Re: namespace std and standard headers
Jay Nabonne wrote:[color=blue]
> On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:
>
>[color=green]
>>If I include both of these headers in a c++ source file:
>>
>>#include <stdio.h>
>>#include <cstdio>
>>
>>would I then be able to do both of:
>>
>>std::printf("Hello, world!\n");
>>printf("Hello, world!\n");[/color]
>
>
> Keep in mind that these will be separate streams
>[/color]
They're the same function, fiddling with the same stream.
--
Pete Becker
Dinkumware, Ltd. ( http://www.dinkumware.com) | 
October 11th, 2005, 08:25 PM
| | | Re: namespace std and standard headers
In article <pan.2005.10.11.17.31.17.234000@sonicNOSPAM.com> ,
Jay Nabonne <jay_nabonne@sonicNOSPAM.com> wrote:[color=blue]
>On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:[color=green]
>> If I include both of these headers in a c++ source file:
>>
>> #include <stdio.h>
>> #include <cstdio>
>>
>> would I then be able to do both of:
>>
>> std::printf("Hello, world!\n");
>> printf("Hello, world!\n");[/color]
>
>Keep in mind that these will be separate streams, flushed at different
>times. So your output may not come out sequentially as you would expect
>without explicit stream flushing.[/color]
Mmmm, I seem to recall there is some issues about whether
the C lib incorp'd herein is extern "C" or "C++"'d but not
whether there are things such as buffering issues. IMO the
above is fine AFAIK.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it? | 
October 11th, 2005, 08:25 PM
| | | Re: namespace std and standard headers
On Tue, 11 Oct 2005 14:47:56 -0500, Pete Becker wrote:
[color=blue][color=green]
>> Keep in mind that these will be separate streams
>>[/color]
>
> They're the same function, fiddling with the same stream.[/color]
Oops. My bad. I was "seeing" std::cout where there wasn't one... :( | 
October 11th, 2005, 08:55 PM
| | | Re: namespace std and standard headers
Jay Nabonne wrote:[color=blue]
> On Tue, 11 Oct 2005 11:59:12 +0000, Simon Elliott wrote:
>[color=green]
> > If I include both of these headers in a c++ source file:
> >
> > #include <stdio.h>
> > #include <cstdio>
> >
> > would I then be able to do both of:
> >
> > std::printf("Hello, world!\n");
> > printf("Hello, world!\n");[/color]
>
> Keep in mind that these will be separate streams, flushed at different
> times. So your output may not come out sequentially as you would expect
> without explicit stream flushing.[/color]
I would not be surprised if cstdio looks like this:
namespace std{
#include <stdio.h>
}
or:
#include <stdio.h>
namespace std{
using printf;
// ......
}
Streams should be synchronized with stdio by default.
std::iostream::sync_with_stdio(false); // unsynchronize
Greetings, Bane. | 
October 11th, 2005, 09:15 PM
| | | Re: namespace std and standard headers
"Branimir Maksimovic" <bmaxa@volomp.com> wrote in message
news:1129063580.663342.46100@o13g2000cwo.googlegro ups.com...
[color=blue]
> I would not be surprised if cstdio looks like this:
> namespace std{
> #include <stdio.h>
> }[/color]
I would. It's dead wrong.
[color=blue]
> or:
> #include <stdio.h>
> namespace std{
> using printf;
> // ......
> }[/color]
That also doesn't conform exactly to the C++ Standard, but
it's a widely adopted practice. It'll probably be made
valid in the next release of the C++ Standard.
[color=blue]
> Streams should be synchronized with stdio by default.
> std::iostream::sync_with_stdio(false); // unsynchronize[/color]
Right, though the "unsynchronize" operation may do
nothing (because there's nothing to be gained).
P.J. Plauger
Dinkumware, Ltd. http://www.dinkumware.com | 
October 12th, 2005, 01:15 AM
| | | Re: namespace std and standard headers
P.J. Plauger wrote:[color=blue]
> "Branimir Maksimovic" <bmaxa@volomp.com> wrote in message
> news:1129063580.663342.46100@o13g2000cwo.googlegro ups.com...
>[color=green]
> > I would not be surprised if cstdio looks like this:[/color][/color]
<snip unlikely contents of cstdio>
[color=blue][color=green]
> > or:
> > #include <stdio.h>
> > namespace std{
> > using printf;
> > // ......
> > }[/color]
>
> That also doesn't conform exactly to the C++ Standard, but
> it's a widely adopted practice. It'll probably be made
> valid in the next release of the C++ Standard.[/color]
Have I misunderstood something here - is the following a correct
program?
#include <cstdio>
int main()
{
printf("Hello world\n"); // or should that be std::printf?
}
I always thought that <cstdio> should put its names in the std
namespace only - not in the global namespace as well. 17.4.1.2/4 would
seem to confirm that. But Comeau online, for example, compiles my
program.
Because the
#include <stdio.h>
namespace std{
using printf;
// ......
}
approach is so widespread I have never been keen to use the <cname>
headers instead of <name.h>. If the compiler is likely to let me get
away with including <cstdio> but leaving the std:: off the front of
printf I might as well use <stdio.h> and have a correct program. But if
<cstdio> is allowed to put its names in the global namespace as well as
the std namespace, I'm worrying over nothing.
Gavin Deane | 
October 12th, 2005, 10:35 AM
| | | Re: namespace std and standard headers
<deane_gavin@hotmail.com> wrote in message
news:1129078861.858088.179870@o13g2000cwo.googlegr oups.com...
[color=blue]
> P.J. Plauger wrote:[color=green]
>> "Branimir Maksimovic" <bmaxa@volomp.com> wrote in message
>> news:1129063580.663342.46100@o13g2000cwo.googlegro ups.com...
>>[color=darkred]
>> > I would not be surprised if cstdio looks like this:[/color][/color]
>
> <snip unlikely contents of cstdio>
>[color=green][color=darkred]
>> > or:
>> > #include <stdio.h>
>> > namespace std{
>> > using printf;
>> > // ......
>> > }[/color]
>>
>> That also doesn't conform exactly to the C++ Standard, but
>> it's a widely adopted practice. It'll probably be made
>> valid in the next release of the C++ Standard.[/color]
>
> Have I misunderstood something here - is the following a correct
> program?
>
> #include <cstdio>
>
> int main()
> {
> printf("Hello world\n"); // or should that be std::printf?
> }[/color]
Should be std::printf, but it sometimes works anyway.
[color=blue]
> I always thought that <cstdio> should put its names in the std
> namespace only - not in the global namespace as well. 17.4.1.2/4 would
> seem to confirm that. But Comeau online, for example, compiles my
> program.[/color]
You're correct about what the C++ Standard *says*. What most
implementations *do* is another matter. And that's because
the C++ committee repeatedly refused to listen to compiler
vendors while drafting the C++ Standard. (Don't forget export
either.)
[color=blue]
> Because the
>
> #include <stdio.h>
> namespace std{
> using printf;
> // ......
> }
>
> approach is so widespread I have never been keen to use the <cname>
> headers instead of <name.h>. If the compiler is likely to let me get
> away with including <cstdio> but leaving the std:: off the front of
> printf I might as well use <stdio.h> and have a correct program. But if
> <cstdio> is allowed to put its names in the global namespace as well as
> the std namespace, I'm worrying over nothing.[/color]
You need to distinguish between what might happen and what you can
rely on. The safe rules are:
-- If you want assuredly to be able to write std::printf, include
<cstdio>.
-- If you want assuredly to be able to write ::printf, include
<stdio.h>.
The C++ Standard says that <cstdio> will not declare ::printf
and that <stdio.h> will also declare std::printf, but neither
of these rules is consistently followed in real life.
HTH,
P.J. Plauger
Dinkumware, Ltd. http://www.dinkumware.com | 
October 12th, 2005, 12:05 PM
| | | Re: namespace std and standard headers
P.J. Plauger wrote:[color=blue]
> <deane_gavin@hotmail.com> wrote in message
> news:1129078861.858088.179870@o13g2000cwo.googlegr oups.com...[/color]
<snip>
[color=blue][color=green]
> > Because the
> >
> > #include <stdio.h>
> > namespace std{
> > using printf;
> > // ......
> > }
> >
> > approach is so widespread I have never been keen to use the <cname>
> > headers instead of <name.h>. If the compiler is likely to let me get
> > away with including <cstdio> but leaving the std:: off the front of
> > printf I might as well use <stdio.h> and have a correct program. But if
> > <cstdio> is allowed to put its names in the global namespace as well as
> > the std namespace, I'm worrying over nothing.[/color]
>
> You need to distinguish between what might happen and what you can
> rely on. The safe rules are:
>
> -- If you want assuredly to be able to write std::printf, include
> <cstdio>.
>
> -- If you want assuredly to be able to write ::printf, include
> <stdio.h>.
>
> The C++ Standard says that <cstdio> will not declare ::printf
> and that <stdio.h> will also declare std::printf, but neither
> of these rules is consistently followed in real life.[/color]
Very helpful, thank you.
What I actually want is to assuredly avoid incorrect code compiling
successfully. It sounds like I will often get away with
#include <cstdio>
int main()
{
std::printf("stuff\n"); // Remembered the std::
std::printf("more stuff\n"); // Remembered again. Well done me.
// Get distracted for a bit ...
printf("Oops! Forgot std::\n"); // But it probably compiles OK
}
If the rule is likely not to be enforced by the compiler, and I am
likely to forget at some point, I might as well just use <stdio.h> and
avoid the whole problem.
Gavin Deane | 
October 12th, 2005, 06:35 PM
| | | Re: namespace std and standard headers
<deane_gavin@hotmail.com> wrote in message
news:1129117912.883923.196030@o13g2000cwo.googlegr oups.com...
[color=blue]
> What I actually want is to assuredly avoid incorrect code compiling
> successfully. It sounds like I will often get away with
>
> #include <cstdio>
>
> int main()
> {
> std::printf("stuff\n"); // Remembered the std::
> std::printf("more stuff\n"); // Remembered again. Well done me.
>
> // Get distracted for a bit ...
>
> printf("Oops! Forgot std::\n"); // But it probably compiles OK
> }
>
> If the rule is likely not to be enforced by the compiler, and I am
> likely to forget at some point, I might as well just use <stdio.h> and
> avoid the whole problem.[/color]
Yep. The use of namespaces to "contain" the Standard C library
was essentially a (predicted) failure.
P.J. Plauger
Dinkumware, Ltd. http://www.dinkumware.com | 
October 12th, 2005, 08:45 PM
| | | Re: namespace std and standard headers
On 12/10/2005, P.J. Plauger wrote:
[color=blue]
> You need to distinguish between what might happen and what you can
> rely on. The safe rules are:
>
> -- If you want assuredly to be able to write std::printf, include
> <cstdio>.
>
> -- If you want assuredly to be able to write ::printf, include
> <stdio.h>.[/color]
And if I need to do both? (This is for legacy code; I can't think of
any reason for using both in new code.)
[color=blue]
> The C++ Standard says that <cstdio> will not declare ::printf
> and that <stdio.h> will also declare std::printf, but neither
> of these rules is consistently followed in real life.[/color]
Yes. I still use Borland C++ Builder a lot, and even the different
versions don't seem to be all that consistent in this respect. You can
even compile the same source file twice and get different results.
--
Simon Elliott http://www.ctsn.co.uk | 
October 13th, 2005, 01:55 PM
| | | Re: namespace std and standard headers
"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:434d72a6$0$38045$bed64819@news.gradwell.net.. .
[color=blue]
> On 12/10/2005, P.J. Plauger wrote:
>[color=green]
>> You need to distinguish between what might happen and what you can
>> rely on. The safe rules are:
>>
>> -- If you want assuredly to be able to write std::printf, include
>> <cstdio>.
>>
>> -- If you want assuredly to be able to write ::printf, include
>> <stdio.h>.[/color]
>
> And if I need to do both? (This is for legacy code; I can't think of
> any reason for using both in new code.)[/color]
Include both headers.
[color=blue][color=green]
>> The C++ Standard says that <cstdio> will not declare ::printf
>> and that <stdio.h> will also declare std::printf, but neither
>> of these rules is consistently followed in real life.[/color]
>
> Yes. I still use Borland C++ Builder a lot, and even the different
> versions don't seem to be all that consistent in this respect. You can
> even compile the same source file twice and get different results.[/color]
That's probably because Borland started out with the RogueWave
library, which tries to do what the C++ Standard requires
with C header namespaces, IIRC. Then they tried STLport instead,
which doesn't. Now they're switching to our library, which can
go either way.
P.J. Plauger
Dinkumware, Ltd. http://www.dinkumware.com | 
October 13th, 2005, 09:45 PM
| | | Re: namespace std and standard headers
On 13/10/2005, P.J. Plauger wrote:[color=blue][color=green]
> > And if I need to do both? (This is for legacy code; I can't think of
> > any reason for using both in new code.)[/color]
>
> Include both headers.[/color]
That's what I hoped you'd say. Thanks for the info.
[color=blue][color=green]
> > I still use Borland C++ Builder a lot, and even the different
> > versions don't seem to be all that consistent in this respect. You
> > can even compile the same source file twice and get different
> > results.[/color][/color]
[color=blue]
> That's probably because Borland started out with the RogueWave
> library, which tries to do what the C++ Standard requires
> with C header namespaces, IIRC. Then they tried STLport instead,
> which doesn't. Now they're switching to our library, which can
> go either way.[/color]
Which versions use your library?
--
Simon Elliott http://www.ctsn.co.uk | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|