What is Std in C++?

Asked

Viewed 451 times

10

I’m learning C++ and I see std being using everywhere.

What is this std? He’s like a library or something different than that?

1 answer

13


It is a namespace for identifiers. You can create yours, any library can have one or more namespaces, which is nevertheless an identifier that gives a surname to other names. You can see more in What is using namespace?.

In the case of std it is the default namespace of the language, so everything that is standard of C++ is placed with that surname. Classic example is the Difference between Std::Cout and Cout?.

The namespace was created in C++ solving some reset problems that existed in C, for example has the function double sin(double) in math.h and including it cannot define another double sin(double) without any build error. C++ already has the library cmath where sin comes in namespace std, then one can do this and each one implemented to give result differently gives a different result:

# include <cmath>

namespace myLib {
    double sin( double x ){
        return x*( 1 + x*x*( -1./6 + x*x*(1./120) ) ) ;  
    }
}

double sin(double x) {
    return x * (1 + x * x * (-1. / 6 + x * x * (1. / 120 - x * x * (-1. / 5040))));  
}

main() {
    using namespace myLib;
    double sin3std = std::sin(3.0);
    double sin3myLib = myLib::sin(3.0);
    double sin3 = sin(3.0);
}

It’s good to read about the controversy: Why is it not good practice to use namespace "Std" in C++?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.