Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C keywords/functions not enclosed in std namespace in C++?

The following piece of code works fine on my local system but threw compile error on an online platform.

#include <iostream>

int32_t time[int32_t(1e5)];

int main()
{
    int32_t n;
    std::cin>>n;
    for(int32_t i=0;i<n;++i)
    {
        int32_t temp;
        std::cin>>temp;
        --temp;
        ++time[temp];
    }
    
    return 0;
}

Complie Error Description

Clearly, the error is generated because of line 3 int32_t time[int32_t(1e5)]; But, I am not able to understand why the compilation error occurred.

I have not included ctime or any such header and even have not unwrapped std namespace. But still, compilation failure occurred.

I have gcc 8.3.0 with __cplusplus 201703 on my system. Not sure what is the version on the online platform.

like image 478
deadLock Avatar asked Jul 19 '20 06:07

deadLock


2 Answers

The standard says that when you include any standard include file it is possible that this will include other include files. It is implementation dependent which ones and how many of them.

The implication is that your code simply must not define any global name that is also standard.

I can understand this seems a difficult requirement (indeed it is) and also that makes one wonder why there are standard include files at all and we don't have simply the whole standard available instead (that's a good question). But none the less this is the situation.

Situation is even worse with POSIX where not only random names are reserved, but also quite a lot prefixes and suffixes; for example code that use any name starting with LC_ followed by an uppercase letter in any way is possibly clashing with #defines related to locale support. Any name that ends with _t is also reserved, not joking. The list is huge.

As a general rule try to define the least possible amount of global names and avoid anything that is also used by the standard library. Even when "it works" on your compiler, your program may find the problem when ported to another compiler (or the next version of the same compiler). Avoiding defining global names makes also easier for your code to be integrated in larger programs with code written by others. Ideally your code should just have one global name (a namespace, a single class or a single function)... unfortunately with C++ you cannot get below that.

Something I remember bumping in when writing small C++ experiments when I usually don't care about these name clashing problems is for example y0 that is a standard Bessel function (this is not a joke; there is a global standard function double y0(double) and any program that uses y0 for anything else at global level is not a valid C++ program).

like image 154
6502 Avatar answered Oct 22 '22 07:10

6502


You include the header file time.h indirectly. In this header file there is a declaration of a function named time that is in conflict with your declaration.

Just change the variable time to another name (time_1).

like image 30
Farhad Sarvari Avatar answered Oct 22 '22 06:10

Farhad Sarvari