Connecting Tech Pros Worldwide Forums | Help | Site Map

namespace std and standard headers

Simon Elliott
Guest
 
Posts: n/a
#1: Oct 11 '05
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

remus.dragos@gmail.com
Guest
 
Posts: n/a
#2: Oct 11 '05

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)!

Simon Elliott
Guest
 
Posts: n/a
#3: Oct 11 '05

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
Jay Nabonne
Guest
 
Posts: n/a
#4: Oct 11 '05

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
Howard Hinnant
Guest
 
Posts: n/a
#5: Oct 11 '05

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
Pete Becker
Guest
 
Posts: n/a
#6: Oct 11 '05

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)
Greg Comeau
Guest
 
Posts: n/a
#7: Oct 11 '05

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?
Jay Nabonne
Guest
 
Posts: n/a
#8: Oct 11 '05

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... :(

Branimir Maksimovic
Guest
 
Posts: n/a
#9: Oct 11 '05

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.

P.J. Plauger
Guest
 
Posts: n/a
#10: Oct 11 '05

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


deane_gavin@hotmail.com
Guest
 
Posts: n/a
#11: Oct 12 '05

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

P.J. Plauger
Guest
 
Posts: n/a
#12: Oct 12 '05

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


deane_gavin@hotmail.com
Guest
 
Posts: n/a
#13: Oct 12 '05

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

P.J. Plauger
Guest
 
Posts: n/a
#14: Oct 12 '05

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


Simon Elliott
Guest
 
Posts: n/a
#15: Oct 12 '05

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
P.J. Plauger
Guest
 
Posts: n/a
#16: Oct 13 '05

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


Simon Elliott
Guest
 
Posts: n/a
#17: Oct 13 '05

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
Closed Thread