简体   繁体   中英

When I should use my own namespaces?

First of all, I can't understand structure of C++ standard library or std. For example, "Hello, world!" programm looks like this:

#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
}

More complex program for generating random numbers and execution time assesment looks like this:

#include <iostream>
#include <random>
#include <chrono>
#include <time.h>

int main() {
    std::chrono::time_point<std::chrono::system_clock> start, end;
    std::tr1::default_random_engine eng(static_cast<unsigned int>(time(NULL)));
    std::tr1::uniform_int<int> unif(0, 99);

    start = std::chrono::system_clock::now();

    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            std::cout << unif(eng) << " ";
        }

        std::cout << std::endl;
    }

    end = std::chrono::system_clock::now();

    int elapsed_milliseconds = std::chrono::duration_cast<std::chrono::milliseconds> (end - start).count();

    std::cout << "Elapsed time: " << elapsed_milliseconds << "ms" << std::endl;

    std::cin.get();
    return 0;
}

By my opinion code like this:

std::tr1::uniform_int<int> unif(0, 99);

or this:

std::chrono::time_point<std::chrono::system_clock> start, end;

looks very ugly. Of course I can use something like this:

using namespace std;
using namespace std::tr1;
using namespace std::chrono;

But this code may cause some problems: Why is "using namespace std" considered bad practice?

I can't understand the reason for creating nested namespaces in standrad library. Is it only one reason conected with name function conflicts? Or something else?

And for my own project when I should use my own namespaces?

Namespace is a logical group. There are no any rules to use namespace. Either you can use or not use namespace. But this is all about organizing your code, library or API. Read this for some best practices by "Herb Sutter" http://www.gotw.ca/publications/mill08.htm

Also you can use using namespace std; for open namespace. There is not any rule says cant. But all are about coding good practices to next programmer to make understanding simple and keep the easy maintainability.

You can use one or more namespace for your program, Specially if you are creating a API or library that going to distribute among public.

And also it helps to keep the module separated in large programs. and allow programmers to work without dependency.

Think Google has two UI teams for web and mobiles.They can use separate namespace for their code modules

Google:Web:UI and Google:Mobile:UI

even they can further extend as

Google:Web:UI:controls and Google:Mobile:UI:controls

Or they can use

Google:controls:Web:UI and Google:controls:Mobile:UI

That is as their wish, comfort and how they think good way to organize. But there can be better ways.

Same story to STDLIB. Developers create namespace structure to organize the modules. But it may not the perfect way. but you have to use it. If you don't like you can aliases it http://en.cppreference.com/w/cpp/language/namespace_alias .

If you are going to create your program. read how other s uses. specially popular C++ project like Linux boost etc..download the code and have a look. If your program is really simple one, you may not need to care so much. But always read and keep good practice (read this book for more information). http://www.gotw.ca/publications/c++cs.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM