简体   繁体   English

什么时候应该使用自己的名称空间?

[英]When I should use my own namespaces?

First of all, I can't understand structure of C++ standard library or std. 首先,我无法理解C ++标准库或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. 我不明白在standrad库中创建嵌套名称空间的原因。 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. 但这一切都与组织代码,库或API有关。 Read this for some best practices by "Herb Sutter" http://www.gotw.ca/publications/mill08.htm 阅读此书,了解“ Herb Sutter”的一些最佳做法http://www.gotw.ca/publications/mill08.htm

Also you can use using namespace std; 您也可以使用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. 您可以为程序使用一个或多个名称空间,特别是在创建要在公共环境中分发的API或库时。

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团队,他们可以为代码模块使用单独的命名空间

Google:Web:UI and Google:Mobile:UI Google:Web:UIGoogle:Mobile:UI

even they can further extend as 即使它们可以进一步扩展为

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

Or they can use 或者他们可以使用

Google:controls:Web:UI and Google:controls:Mobile:UI Google:controls:Web:UIGoogle: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. 与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 . 如果您不喜欢,可以为它添加别名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. 特别流行的C ++项目,例如Linux boost下载代码并查看。 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 http://www.gotw.ca/publications/c++cs.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么时候应该在C ++中使用自己的破坏函数? - When should I use destruction function of my own in C++? 当我可以使用默认的TimerQueue时,为什么要使用自己的TimerQueue? - Why should I use my own TimerQueue when I can use the default one? 创建自己的数据结构时,是否应该使用迭代器或索引来提供外部访问? - When creating my own data structure, should I use iterators or indices to provide access from the outside? 什么时候应该使用CUDA的内置warpSize而不是我自己的适当常数? - When should I use CUDA's built-in warpSize, as opposed to my own proper constant? 我应该在实现文件中使用未命名的命名空间吗? - Should I use unnamed namespaces in implementation files? 是否应该使用std :: vector +我自己的大小变量? - Should I use std::vector + my own size variable or not? 我应该何时定义自己的副本ctor和赋值运算符 - When should I define my own copy ctor and assignment operator 为什么以及如何在 C++ 中使用命名空间? - Why and how should I use namespaces in C++? 我应该扩展QT小部件还是使用我自己的包含小部件的类作为成员? - Should I extend QT widgets or use my own classes containing the widget as a member? 我应该使用什么数据结构来创建自己的“BigInteger”类? - What data-structure should I use to create my own “BigInteger” class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM