Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to put a function declaration within an unnamed namespace?

Tags:

c++

namespaces

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?

like image 317
helpermethod Avatar asked Dec 13 '10 12:12

helpermethod


People also ask

What is true about an unnamed namespace?

Unnamed NamespacesThey are directly usable in the same program and are used for declaring unique identifiers. In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. The name of the namespace is uniquely generated by the compiler.

What is the use of unnamed namespace?

An anonymous namespace makes the enclosed variables, functions, classes, etc. available only inside that file. In your example it's a way to avoid global variables. There is no runtime or compile time performance difference.

Why is an unnamed namespace used instead of static?

Unnamed namespaces are typically used when you have a lot of content that you want to ensure stays local to a given file, as it's easier to cluster such content in an unnamed namespace than individually mark all declarations as static .

What does anonymous namespace mean?

November 19, 2020. Anonymous namespaces in C++ allow you to define locally-visible-only artifacts in C++. the static keyword provides equivalent (depending on C++ version) locally-visible-only variables and functions. //impl.cpp. //neither can be used externally.


2 Answers

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.

like image 192
Marcelo Cantos Avatar answered Sep 21 '22 07:09

Marcelo Cantos


An unnamed namespace behaves as if it was replaced with a namespace with a uniquely generated name immediately followed by a using directive.

This means that your function declaration belongs to a namespace exactly as if the namespace actually had a name. As such, its definition should live in the same namespace : either simultaneously declare and define the function, or add an enclosing namespace {} around the definition (which works because all occurrences of the unnamed namespace in a translation unit refer to the same namespace).

namespace {

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

}
like image 40
icecrime Avatar answered Sep 23 '22 07:09

icecrime