2

I have this in a .cpp file:

namespace {
    std::string CListName;
}

namespace EXAMPLE_NS {
    CListName = "ListName";
    ...

But Eclipse highlights the assignment as a Syntax Error. I take it you can't use an anonymous namespace from another namespace?

1
  • So I moved the assignment statements inside the constructor within the EXAMPLE_NS, seems to have sorted out the eclipse error and clicking the var name highlights the anonymous name also. Commented Aug 25, 2011 at 17:09

3 Answers 3

7

CListName = "ListName"; is a statement (specifically, it's an assignment expression, which is an expression statement).

A statement cannot appear at namespace scope; you need to put the statement into a function.

Sign up to request clarification or add additional context in comments.

Comments

3

What you have there is a syntax error, but it's not a problem with the namespace. What you have there is an assignment statement outside of any function or method. GCC tells me:

error: expected constructor, destructor, or type conversion before ‘=’ token

When trying to compile your snippet. Clang has a better message:

error: C++ requires a type specifier for all declarations
    CListName = "ListName";
    ^~~~~~~~~
1 error generated.

It all means the same thing, though - you can't have statements outside of functions!

Comments

-1

Yes, you can. Anonymous namespaces are the C++ way to remove the external linkage of the objects declared inside.

So I'd say it is a bug in the Eclipse syntax highlight.

Of course, you are doing the assignment into a function, not typed here for brevity, aren't you?

1 Comment

There is no bug here. You can't just assign things at namespace scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.