是否可以将函数声明放在未命名的命名空间中? [英] Is it possible to put a function declaration within an unnamed namespace?

查看:64
本文介绍了是否可以将函数声明放在未命名的命名空间中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一组功能的文件.对于其中一个函数,我想编写一个辅助函数,该函数基本上使用char *并跳过所有空格.

I have a file with a set of functions. For one of the functions, I want to write a helper function which basically takes a char * and skips all whitespaces.

这是我认为应该完成的方式:

Here's how I thought it should be done:

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) {}
}

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

But this gives me a compiler error. Do I need to put the definition within the unnamed namespace?

推荐答案

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

You have to define it in the anonymous namespace as well:

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) {}
    }
}

但是,除非存在循环依赖性,否则我不确定该值是什么.只需一次声明和定义函数.

But unless there is a cyclic dependency, I'm not sure what the value of this is. Just declare and define the function in one go.

这篇关于是否可以将函数声明放在未命名的命名空间中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆