Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 Anonymous Namespace + Refactorings
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

akhtar
Starting Member

USA
1 Posts

Posted - Jul 14 2008 :  1:56:27 PM  Show Profile  Reply with Quote
I have created an example of what I think is incorrect functionality.

namespace {
    // 1) Type the declaration
    void MyFunction();

    // 2) Right click "MyFunction", and apply the VAX Create Implementation refactoring
}


namespace {

}
    // 3) Note that the generated implementation is not in the existing anonymous namespace or wrapped in its own.

void MyFunction()
{

}

Jon S Akhtar

feline
Whole Tomato Software

United Kingdom
18943 Posts

Posted - Jul 15 2008 :  10:33:16 AM  Show Profile  Reply with Quote
I am seeing the same effect, but this does not seem to matter. Using VS2005 I have added the following code to a header file:

namespace
{
	class testCreateImpClassAnonymousNamespace
	{
		void AnonymousMemberFunctionOne();
	};
}


and this code to the matching cpp file:

namespace
{
}

void testCreateImpClassAnonymousNamespace::AnonymousMemberFunctionOne() { }


and this compiles perfectly happily. As soon as I give the namespace in the header file a name the code no longer compiles.

Does an anonymous namespace actually have any effect at all?

zen is the art of being at one with the two'ness
Go to Top of Page

mwb1100
Ketchup Master

82 Posts

Posted - Jul 15 2008 :  1:44:57 PM  Show Profile  Reply with Quote
quote:
Originally posted by feline


Does an anonymous namespace actually have any effect at all?


An anonymous namespace scopes the enclosed declarations to the current compilation unit (similar to using the variant of 'static' that prevents a name declared at file scope from having external linkage).

Some (most?) C++ experts suggest that an anonymous namespace should be used in preference to using the static keyword to keep items 'private' to the current compilation unit. I'm not entirely sure if there's any benefit other than it reduces the overloaded use of the 'static' keyword (which is actually a good enough reason for me, but not good enough to call out for other people's code).

When an anonymous namespace is used, there's an implicit 'using' directive that brings those names into scope for the compilation unit.

That's why the compilation works even when the definition of the function/variable is not enclosed in the namespace braces. So the code that VAX creates works, but I can understand that someone might want the definition to end up within the namespace. It would be my preference.

Go to Top of Page

feline
Whole Tomato Software

United Kingdom
18943 Posts

Posted - Jul 15 2008 :  3:19:41 PM  Show Profile  Reply with Quote
That is interesting, thank you. Since anonymous namespaces clearly have a point and a use I have put in a bug report for this:

case=18502

As a temporary workaround you can use VA Outline to move the function implementations into the namespace.

zen is the art of being at one with the two'ness
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000