I'm a bit confused about using-declarations. I understand that using foo::bar;
imports the symbol bar
from namespace foo
into the current namespace, but does this happen statically or dynamically?
More specifically, do using-declarations lead to an overhead? Would it be possible to import different symbols with the same name depending on a condition? (that would be bad practice, but I'm curious all the same)
It feels like it should be static, but I can't find anything to confirm this...
Namespace resolution happens at compile time. You can not change them dynamically at run time.
One way to import different symbols depending on condition is to use preprocessor directives and macros:
#ifdef USEA
using a::f;
#else
using b::f;
#endif
using
in c++
is compile-time directive, ie it only affects how compiler resolves names during compilation
Would it be possible to import different symbols with the same name depending on a condition?
Depends what condition you are talking about. In case preprocessor's condition like #ifdef
yes it is possible. If you think about a run-time condition then no, using
is not performed at run time
Using declaration looks like any other declarations. It has its own scope depending on where it is written. You may not dynamically declare a variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.