10

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?

helpermethod
  • 59,493
  • 71
  • 188
  • 276

3 Answers3

14

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.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • 7
    I like when you can read source code upside down, i.e. when function A calls function B, function B also follows function A in the source code. In Java this is no problem but I guess in C++ defining it directly is the way to go. – helpermethod Dec 13 '10 at 12:13
5

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

}
icecrime
  • 74,451
  • 13
  • 99
  • 111
1

You probably need to see this topic as well:

Superiority of unnamed namespace over static?

BTW, why this function:

void SkipWhitespace(const char *s);

Why not this:

void SkipWhitespace(std::string &s);

??

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Sry I didn't mention that it was a homework assignment, where using the std::string class is prohibited. – helpermethod Dec 13 '10 at 12:18
  • 2
    @Helper lol, a C++ assignment with std::string prohibited. Obviously, you have some fun courses... – stefaanv Dec 13 '10 at 12:26
  • 2
    Yeah, sadly it's one of those weird C/C++ course where you end up don't knowing either language :-(. – helpermethod Dec 13 '10 at 12:28
  • @stefaanv : this is the usual situation in India here where students are taught C in the name of C++. But what happened to Germany? I didn't expect it. – Nawaz Dec 13 '10 at 13:52
  • @Nawaz: obviously off-topic, but you got me interested: what happened in my neighbour-country that you didn't expect. The media hasn't informed us yet. Or are you talking about the Amsterdam scandal (Holland)? – stefaanv Dec 14 '10 at 10:19
  • @stefaanv : No, I'm not talking about anything of that sort. I'm just concerned about (in fact "worried" about) the education and the educators here in India. I graduated from a top institute of the country, yet I started knowing the real C+++ when I joined communities such as this, and when I came across books like Modern C++ Design by Andrei Alexandrescu. Now, I feel that I just wasted my years while knowing C in the name of C++. Even that C never talked anything about undefined, implementation defined, unspecified, and so on. Nothing. Such a waste of time! – Nawaz Dec 14 '10 at 10:30
  • @Nawaz: Oh I see, actually it's not that different over here (eventhough C/C++ wasn't even thought at the time), but we didn't went much further then the available language elements and constructs and some basic exercises. – stefaanv Dec 14 '10 at 11:03
  • @stefaanv : Hmm. I see. But anyway, now I'm getting what I always wished for. Just wish me good luck. :-) – Nawaz Dec 14 '10 at 11:19