algorithmc++11namespacesstdcmath

Why should we use std name space before functions of <algorithm> header and should not use it before functions of <cmath> header?


I was wondering why we should use the std namespace before function of <algorithm> header like max() and we are not obliged to use it before functions of <cmath> header like round()?

Examples: max() round()

Thanks


Solution

  • <cmath> is essentially a wrapper around math.c from the C standard library.

    This header was originally in the C standard library as <math.h>. (source: https://en.cppreference.com/w/cpp/header/cmath)

    C++ is a (almost) a superset of C, meaning that a C++ compiler should compile almost any program written in C. For this reason functions from the C standard library do not belong to the std namespace, which is a C++'s concept. Other examples: printf, fopen. Of course you can freely mix C++ standard library with C standard library functions, constants, macros etc.

    For incompatibilities between C and C++ see for example Where is C not a subset of C++?