Ashot
Ashot

Reputation: 10989

C++: namespace specifier for classes and functions

Why for using vector class I should write namespace specifier std::vector but for std::find function I can omit std:: ? What is the difference? Both are defined in std namespace, right?

Upvotes: 0

Views: 360

Answers (4)

Benius
Benius

Reputation: 143

At the start, you include libraries to your program (std namespace) Then afterward you don't need to use all the time the word std. But in reality all the objects equal std::cout std::cin

std is one of the fundamental libraries of c++

Upvotes: 1

Andy Prowl
Andy Prowl

Reputation: 126582

Why for using vector class I should write namespace specifier std::vector but for std::find function I can omit std:: ?

In general, that is not true.

What is the difference. Both are defined in std namespace, right?

Yes, and there is no difference in principle. Per Paragraph 17.6.1.1/2 of the C++11 Standard:

All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std. [...]

However, you must keep in mind that exceptions are allowed by the rule of Argument-Dependent Lookup: thanks to this lookup technique, it is not necessary to explicitly specify the namespace of a function, because the name will be looked up in the namespace of the arguments you are supplying to that function.

Thus, if the function happens to be defined in the same namespace as any of its arguments, it will be found by the compiler even though its name is not qualified with the namespace it belongs to:

namespace N
{
    struct X { };

    void foo(X) { }
}

int main()
{
    N::X x;
    foo(x); // OK
}

Thus, if you are calling std::find() with some arguments that are instances of types defined in the std namespace, you won't need to qualify the call to find() with std::. For instance:

#include <vector>
#include <algorithm>
#include <iostream>

int main()
{
    std::vector<int> v { 1, 2, 3 };
    auto i = find(v.begin(), v.end(), 2); // ADL applies if the result of
                                          // v.begin() and v.end() is an
                                          // iterator whose type is defined 
                                          // in the std namespace!
    std::cout << *i;
}

Obviously, this can't apply to functions that accept no argument. In that case, the namespace qualification is always necessary, unless of course the point of invocation is inside that namespace (although you can't add anything to the std namespace, some templates from the std namespace can be specialized).

Upvotes: 4

Nick BL
Nick BL

Reputation: 373

For some reason the STL chose to expose many of the algorithms (in ) in the global namespace. that is why you can just write find(...) or transform(...). Vectors are not exposed so you must either do std::vector<>(). Or you can just put using std::vector; at the top of your file

Upvotes: 0

zaufi
zaufi

Reputation: 7129

It depends on how you call std::find... for example if you pass some iterators from std container, this function will be found by ADL, otherwise you have to add std:: to call it.

Upvotes: 1

Related Questions