Is this supposed to compile?

Using MinGW GCC 4.6.2, I found that my code still compiles if I don't have std:: in front of some of the functions (specifically, it seems, the ones that are also part of the C library).

For instance, this compiles and runs with no errors or warnings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

int main()
{
    // No std:: on any of these
    clock_t start = clock();
    double blah = sqrt(4.0);
    srand(time(0));
    int random = rand();
    double blahy = log10(10.0);
    double log10; // This works (as I would expect)
    //log10 = log10(100.0); // Error if I uncomment (as I would expect)
    log10 = std::log10(100.0); // NOW I have to use std::

    // Yes, I know this is C++, not C, but this is just to show that this works without std::
    printf("%ld\n%f\n%d\n%f\n%f\n", start, blah, random, blahy, log10);

    system("pause"); // Ewww, system()...
    return 0;
}

This also compiles (as expected) if I stick std:: in front of everything (well, not everything, of course, but you get the point).
Was that supposed to compile?
It looks you have pretty much a C based program there. I believe clock_t is C++ based so you might need a std:: namespace in front of that but everything else looks like C code.

Why don't you make things easy on yourself and just add:

using namespace std;

before your code.


That way you don't have to scope the std:: namespace every time you need to use it.

Edit: printf is more C than C++. C++ uses cout as the standout output.

C is really powerful but it's also weird. Pretty much everything is treated like a file and it allows you more control which also leads to more mistakes so I would try to learn C++ instead.
Last edited on
Hm, AFAIK, using the cxxx headers should be wrapping the C stuff in namespace std, so I don't really know why that would be happening.

But like IceThatJaw said, that's basically C, not C++. If that's what you were trying to do, then that's fine.
The standard doesn't require, but also doesn't forbid <cstdio>, <cstdlib>, and <ctime> to place symbols in both the std namespace and the global namespace (where <stdio.h>, <stdlib.h> and <time.h> would put them).

So, many compilers (or, technically, standard library implementations) do both. But to be totally portable, you need std:: in front of every standard library symbol in that program.
Cubbi, you speak the truth but when someone has to ask questions like this, they shouldn't have to worry about maximum portability. : )
If one wasn't worried about portability, it seems like one wouldn't ask the question. After all, if you're not worried about what's portable and what's not you can just go with whatever works on your compiler without a care for whether it should or shouldn't work that way.

Btw, whether printf is "more C than C++" doesn't really matter. It is C++. (And in many cases the C-oriented i/o functions are more efficient than C++ counterparts, meaning there is plenty of reason to use them in scenarios where i/o performance is paramount.)
If you want to talk about maximum performance then why even use C or C++? Let's just code in assembly.

Seriously, TS is obviously not a veteran programmer (neither am I) and I am just trying to point out the basic nature of things.

When I see people using printf, scanf, and including .h libraries I try to remind them that it is not the standard C++ way.

This is a C++ forum isn't it?
Last edited on
Topic archived. No new replies allowed.