Can I put a complete namespace in a specific section? - c++

I am working on an embedded project and have to put a complete section of our code in a specific memory region. We are using avr-gcc.
The normal way to go is to tell GCC to put the function in a section with:
__attribute__((__section__(".text_sdram"))) void foo(void);
However, this would cost us a lot of effort plus the chance to "forget" one function.
We are using C++ and all the function that have to be in .text_sdram is put in a specific namespace.
Is it possible to put a complete namespace in a specific section?
Thanks in advance for your answers.
Pieter

According to function attributes the visibility attribute can be attached to a namespace, but it doesn't look like anything else can. You could try attaching the section attribute to your namespace using the same syntax as described for visibility. I don't expect it to work, and if it doesn't I'm fairly sure there's not a good answer. If your namespace is all defined in a single module, or can be rearranged that way, you can probably solve this with a linker script.

Related

C++ detect if global variable was declared

Is it possible to use SFINAE like boost's has_member or other C++ standard complient means to check if a specific variable was declared in global scope?
This means without declaring a macro beforehand to check this or using compiler specific additions like MSVC's __if_exists. GCC like weak symbol declarations are also no option.
A solution using C++11 or earlier would be perfect for me. Other solutions are also welcome.
P.S.: It can be assumed that the type of the variable is known.
Background:
I am currently trying to target many differnent Arduino plattform in a generic way. I know that the special pins are declared as static const uint8_t but I do not know if for example SDA1 or just SDA was declared. To support newer targets without writing a new board definition each time I would like to check a given set of possible pins and support those if declared. This should work out of the box with the Arduino IDE, hence no compiler specific features, extra programs or other fancy additions should be used. I know that for most of those variables there is also a macro declared which indicates those present. Nevertheless, the cleaner solution in my point of view is to check directly, whether the pin variable was declared or not, as the macro names tend to be target specific whereas the pin variable name and declaration looks quite stable. The proper solution would be to enfore naming conventions and macro defintions to support such checks, of course. The effort to push idea this to all Arduino core projects is, however, too much for me.
P.P.S: Please do not just down vote or comment this for being a xy-question. My example is just one of many. Any other API like multi-library target project could make use of such a solution as well. Even a clear "no, that's not possible" would save at least a lot of time searching for such a solution.
I think you can best solve this using a build step, such as a configure script that tries to compile code using the global variables in question. It can then determine whether the build fails or succeeds, and define proper makros to handle the cases in question in your code.

How does one know if a namespace in a dynamic or static library is already used?

In languages like java, a package name is a domain name, like com.foobar.mystuff.
So if you own com.foobar, it is highly unlikely that someone would have used the package name com.foobar, you can be reasonably sure there will not be a collision.
But in c++, you can choose any namespace name. How do you know if a library you are linking to, is not already using a particular namespace name ? Is there a way to test it, especially if you don't have access to source code or documentation ? Is there some guideline to avoid this problem ?
Is there some guideline to avoid this problem?
While it may surprise you to find that one of the third party libraries you are using uses a namespace as your application, it shouldn't cause too much problem, if any.
In the worst case scenario, you'll have to create a namespace that is specific to your application and create nested namespaces under the main namespace.

Vim: Highlight C++ variables using scope?

I would like to have C++ variables highlighted by scope. E.g. variables should have different coloring depending on file, class, global or local scope. Is it possible?
UPDATE: External helpers (e.g. ctags/clang and vim scripts) are welcome.
UPDATE 2: This really should be possible using libclang. Take a look at Doug Gregor presentations here: http://llvm.org/devmtg/2010-11/ I think just no one has done it yet... or?
http://www.vim.org/scripts/script.php?script_id=2646
From the script website:
This set of scripts is designed to increase the number of highlighting groups used by Vim. This makes it quicker and easier to spot errors in your code. By using ctags and parsing the output, the typedefs, #defines, enumerated names etc are all clearly highlighted in different colors.
There are some screenshots available to show the comparison.
http://sites.google.com/site/abudden/contents/Vim-Scripts/ctags-highlighting
One trick I have seen but don;t use:
If you declare your variables with the appropriate prefix g_, m_ etc.. You can use this to get vim to colour them differently:

Can intellisense be enabled in VS2008 within preprocessor directive blocks like #ifndef ... #endif

While working within C++ libraries, I've noticed that I am not granted any intellisense while inside directive blocks like "#ifndef CLIENT_DLL ... #endif". This is obviously due to the fact that "CLIENT_DLL" has been defined. I realize that I can work around this by simply commenting out the directives.
Are there any intellisense options that will enable intellisense regardless of directive evaluation?
By getting what you want, you would lose a lot.
Visual C++ IntelliSense is based on a couple major presumptions
1. that you want good/usable results.
2. that your current IntelliSense compiland will present information related to the "configuration" you are currently in.
Because your current configuration has that preprocessor directive, you will not be able to get results from the #ifndef region.
The reason makes sense if you think it through. What if the IntelliSense compiler just tried to compile the region you were in, regardless of #ifdef regions? You would get nonsense and non-compilable code. It would not be able to make heads or tails of your compiland.
I can imagine a very complex solution where it runs a smaller (new) parse on the region you are in, with only that region being assumed to be part of the compiland. However, there are so many holes in this approach (like nothing in that region being declared/defined) that this possible approach would immediately frustrate you, except in very very simple scenarios.
Generally it's best to avoid logic in #ifdef regions, and instead to delegate the usage of parameterized compilation to entire functions, so that the front-end of the compiler is always compiling those modules, but the linker/optimizer will select the correct OBJ later on.
Hope that helps,
Will
Visual Studio 6.0 has a little better support for C++ in some arena's such as this. If you need the intellisense then just comment it out temporarily, build and then you should have intellisense. Just remember to recomment it when you're through if that was your intent.
I just wish Intellisense would work when it SHOULD in VS2008. MS "workarounds" don't work (deleting .ncb files) most of the time. Oooh,
here's another SO discussion..., let's see what IT has to say (I just love SO)
I'm often annoyed by that too ... but I wonder whether intellisense would actually be able to provide any useful information, in general, within a conditioned-out block?
The problem I see is that if the use of a variable or function changes depending on the value of a preprocessor directive then so may it's definition. If code-browsing features like "go to definition" were active within a conditioned-out block would you want them to lead to the currently-enabled definition or to one that was disabled by the same preprocessor conditions as the disabled code you're looking at?
I think the "princple of least surprise" dictates that the current behaviour is the safest, annoying though it is.
Why you want to do explicitly in the code?
There is already cofiguration setting in VS and the way you can enable and disble the intellisense.
see the link.
http://msdn.microsoft.com/en-us/library/ms173379(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ks1ka3t6(v=VS.80).aspx
This link may help you.

Why is there no way to undo 'using' in C++?

I've often found myself wanting a way to undo the effect of a using statement or to include all of a namespace (such as std) but exclude a bit to be replaced (such as cout). For some reason this isn't possible. I am wondering if anyone knows why it was decided not to add this ability to the language? Is there some technical reason? I assume it wasn't just forgotten since it doesn't seem slated for C++0x either.
Just to clarify, I'm not looking for workarounds since Google can show me those. I'm looking for an explanation of why this is impossible, and why it was not considered (as far as I can tell) for inclusion in 0x.
A using directive brings a name or set of names into a given declarative scope.
You can't "un-using" for the same reason that you can't say
int x = 42;
// and later
[remove name x somehow]
There's no way to unintroduce names from a scope at all in C++, regardless where those names came from.
Given that it would overcomplicate name lookup (since names could be both added and removed from a scope), unless there is a really compelling use case, it's unlikely to be considered as a potential language feature.
This is because the using directive is not meant to be used for native C++ code. It was intended to help migrate C code to C++. In this context, "un-using" doesn't make sense.
-edit-
I should have been more specific. In this particular case, it looks like mstearn is using the using directive to include the std namespace globally. Doing this is generally a bad idea because it results in global namespace pollution, and should only be done in certain circumstances, like transitioning from another language to C++.
There are other situations where utilizing the using directive is fine (within a function, namespace composition). However "un-using" doesn't make sense in these situations either.
Mostly because there are workarounds that are sufficiently simple and straightforward that virtually nothing would be gained by including a "feature" for that specific purpose. Though I'm not sure he ever states it directly, I think you could argue that one of the guidelines in the design of C++ has always been to prefer general mechanisms to special-purpose ones, so using the existing scope system (for example) makes more sense than adding some special way to remove something from a scope after it's been introduced.