有没有一种方法可以不使用命名空间中的项目?

时间:2020-06-11 16:05:45

标签: c++ namespaces std

我了解到打字

using namespace std;
程序开头的

是个坏习惯,因为它包含名称空间中的每个函数。如果发生名称冲突,则有可能导致错误。

我的问题是,有没有一种方法可以指定您不想要使用的命名空间功能?是否有一些声明,例如

not_using std::cin;

能做到这一点吗?

3 个答案:

答案 0 :(得分:12)

您不能这样做(先包含所有内容,然后有选择地排除某些内容)。

您的选择是:

1)始终明确限定名称。像std::vector<int> v;

2)使用using namespace std;

输入所有名称

3)仅输入您需要的名称,例如using std::vector;,然后执行vector<int> v;-不会输入除“ vector”以外的其他名称。

注意:using namespace std;不必在全局范围内污染整个文件。您可以根据需要在函数内部进行操作:

void f() {
    using namespace std;
    // More code
}

这样,只有f()会提取其本地范围内的所有名称。 using std::vector;等也是如此。

答案 1 :(得分:2)

您可以$table->id(); $table->date('added_on')->default(DB::raw('CURRENT_TIMESTAMP')); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->string('photo')->nullable(); $table->string('station')->nullable(); $table->float('amount', 10, 2)->nullable(); $table->tinyinteger('status')->default(0); $table->integer('role_id')->default(0); $table->string('phone')->default(0); $table->rememberToken(); $table->softDeletes(); $table->timestamps(); 仅是您想要无限制访问的名称。

https://en.cppreference.com/w/cpp/language/namespace

答案 2 :(得分:1)

使用以下语法代替使用全局名称空间范围: 例如 : std :: cout:

有关更多示例,请阅读: http://www.cplusplus.com/doc/tutorial/namespaces/