如何从 std 标头隐藏全局函数

问题描述 投票:0回答:1

我想从我的程序打印到标准输出。但是当我包含

iostream
时,许多其他函数会直接注入到全局空间。

#include <iostream>
int main() {

    std::cout << "test";

    getc(0); // visible WITHOUT namespace
    getchar(); // visible WITHOUT namespace
}

有什么方法可以隐藏 std 标头中不必要的函数吗?

我需要隐藏这个函数,因为我无法创建自己的具有相似名称的函数,而且它还会阻塞智能感知建议。

c++ namespaces c++20 std
1个回答
0
投票

以下是如何使用“using”指令将特定函数引入全局命名空间的示例:

#include <iostream>

using std::cout;
using std::endl;

int main() {
    cout << "test" << endl;

    getc(0);      // visible without namespace
    getchar();    // visible without namespace
}

在此示例中,使用

cout
指令将
endl
using
引入全局命名空间。 然而,值得注意的是,由于可能存在命名冲突,通常不鼓励将整个命名空间纳入全局命名空间。

© www.soinside.com 2019 - 2024. All rights reserved.