Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given that <code>using</code> declarations at class scope are not inherited, this could work. The name would only be valid inside that class declaration, or inside the declarations of nested classes. But I think it's sort of overloading the concept of a class with an idea that should be larger.</p> <p>In Java and Python individual files are treated in a special way. You can have <code>import</code> declarations that inject names from other namespaces into the file. These names will (well, not exactly with Python, but it's too complicated to explain here) only be visible within that file.</p> <p>To me that argues for this sort of ability not being tied to a class declaration, but given a scope of its own instead. This would allow injected names to be used in several class declarations if it made sense, or even in function definitions.</p> <p>Here is an idea I prefer because it allows these things while still giving you the benefits of a class level using declaration:</p> <pre><code>using { // A 'using' block is a sort of way to fence names in. The only names // that escape the confines of a using block are names that are not // aliases for other things, not even for things that don't have names // of their own. These are things like the declarations for new // classes, enums, structs, global functions or global variables. // New, non-alias names will be treated as if they were declared in // the scope in which the 'using' block appeared. using namespace ::std; using ::mynamespace::mytype_t; namespace mn = ::mynamespace; using ::mynamespace::myfunc; class AClass { public: AClass(const string &amp;st, mytype_t me) : st_(st), me_(me) { myfunc(&amp;me_); } private: const string st_; mn::mytype_t me_; }; // The effects of all typedefs, using declarations, and namespace // aliases that were introduced at the level of this block go away // here. typedefs and using declarations inside of nested classes // or namespace declarations do not go away. } // end using. // Legal because AClass is treated as having been declared in this // scope. AClass a("Fred", ::mynamespace::mytype_t(5)); // Not legal, alias mn no longer exists. AClass b("Fred", mn::mytype_t); // Not legal, the unqualified name myfunc no longer exists. AClass c("Fred", myfunc(::mynamespace::mytype_t(5)); </code></pre> <p>This is analogous to declaring a block for local variables in a function. But in this case you are declaring a very limited scope in which you will be changing the name lookup rules.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload