c++ - 是否可以将函数声明放在未命名的命名空间中?

标签 c++ namespaces

我有一个包含一组函数的文件。对于其中一个函数,我想编写一个辅助函数,它基本上采用 char * 并跳过所有空格。

我认为应该这样做:

namespace {
    const int kNotFound = -1;

    void SkipWhitespace(const char *s); // forward declaration - doesn't seem to work?
}

void foo(const char *s1, const char *s2) {
    // do some stuff

    SkipWhitespace(s1);
    SkipWhitespace(s2);

    // continue with other stuff
}

void SkipWhitespace(const char *s) {
    for (; !isspace(s); ++s) {}
}

但这给了我一个编译器错误。我是否需要将定义放在未命名的命名空间中?

最佳答案

您还必须在匿名命名空间中定义它:

namespace {
    ...
    void SkipWhitespace(const char *s); // forward declaration - doesn't seem to work?
}

void foo(const char *s1, const char *s2) {
    ...
}

namespace {
    void SkipWhitespace(const char s*) {
        for (; !isspace(s); ++s) {}
    }
}

但除非存在循环依赖,否则我不确定这有什么值(value)。只需一次性声明和定义函数。

关于c++ - 是否可以将函数声明放在未命名的命名空间中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4428589/

相关文章:

c++ - 如何使用 Visual Studio 2017 编译 CEF 库?

r - loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) "Rcmdr"中的 ‘car’ 错误加载失败 :

c# - 生成 WCF 代理的命名空间困境

c# - 在目录中组织命名空间和类

c++ - 为什么需要从重载括号运算符返回引用(OR : why is an lvalue not returned otherwise)?

c++ - 为什么 OpenCV KdTree 在 C++ 中总是返回相同的最近邻居?

c++ - 强制派生类实现私有(private)方法

c++ - 指导编译器如何优化我的代码

Spring GCP 数据存储接口(interface) : setting the namespace?

python - `__import__(' pkg_resources').declare_namespace(__name__)` 有什么作用?