Questions tagged [using-directives]

The `using` directive, available in several languages including C# and C++, introduces members of a namespace into the current identifier search scope.

The C++ using directive allows the names in a namespace to be used without the namespace-name as an explicit qualifier. Of course, the complete, qualified name can still be used to improve readability.

// C++ Example:
using std::cout

The C# using directive is used to qualify namespaces and create namespace or type aliases. The scope of a using directive is limited to the file in which it appears.

// C# Example:
using System.Text;
using Project = PC.MyCompany.Project;

Creating an alias for a namespace or a type in C# is called a "using alias directive", and is illustrated in the code sample below:

namespace PC
{
    // Define an alias for the nested namespace. 
    using Project = PC.MyCompany.Project;
    class A
    {
        void M()
        {
            // Use the alias
            Project.MyClass mc = new Project.MyClass();
        }
    }
    namespace MyCompany
    {
        namespace Project
        {
            public class MyClass { }
        }
    }
}
289 questions
18
votes
3 answers

How to properly bind scope between directive and controller with angularJS

I'm trying to generate an n-level hierarchical unordered list with anugularJS, and have been able to successfully do so. But now, I'm having scope issues between the directive and controller. I need to change a scope property of the parent from…
user2165994
  • 275
  • 2
  • 3
  • 6
17
votes
6 answers

What's the purpose of: "using namespace"?

There are convincing arguments against using namespace std, so why was it introduced into the language at all? Doesn't using namespace defeat the purpose of namespaces? Why would I ever want to write using namespace? Is there any problem I am not…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
17
votes
1 answer

Using directive to specify class alias in C++/CLI

In C#, there are three types of using directives: using System; // Specify Namespace using Diag = System.Diagnostics; // Specify Namespace Alias using DBG = System.Diagnostics.Debug; // Specify Class Alias In C++/CLI, I know the equivalents to the…
David Yaw
  • 27,383
  • 4
  • 60
  • 93
17
votes
12 answers

What requires me to declare "using namespace std;"?

This question may be a duplicate, but I can't find a good answer. Short and simple, what requires me to declare using namespace std; in C++ programs?
vette982
  • 4,782
  • 8
  • 34
  • 41
16
votes
7 answers

where to put using namespace std;

I'm wondering where to put using namespace std;. I saw a code with the using namespace std; in the int main(){} but I was putting it after #include . Where should I put it and does it make any difference where I put it?
Hikari Iwasaki
  • 871
  • 2
  • 9
  • 11
16
votes
4 answers

Invoking begin and end via using-directive?

The established idiom for invoking swap is: using std::swap swap(foo, bar); This way, swap can be overloaded for user-defined types outside of the std namespace. Should we invoke begin and end in the same fashion? using std::begin; using…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
16
votes
4 answers

In C#, is it more performant to use fully qualified names vs the 'using' directive?

In C#, when you add a using directive for a namespace, it gives you access to all the types in that specific namespace. However, if the namespace has a lot of types and I only need one particular one, I often just use the fully qualified name…
johntrepreneur
  • 4,514
  • 6
  • 39
  • 52
16
votes
2 answers

C++11 `using` keyword: specialize template alias of template parameter

I had a problem today using the using keyword in C++11. I decided to use another approach now (added as comments in the example below). You can think of X as a matrix, of Y as a mixin and the aim is to access the tranposed matrix type of X in Y.…
Markus Mayr
  • 4,038
  • 1
  • 20
  • 42
16
votes
7 answers

What's this C# "using" directive?

I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that all about?
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
14
votes
1 answer

Can class members be defined outside the namespace in which they are declared?

Sometimes I find code like the following (actually some class-wizards create such code): // C.h namespace NS { class C { void f(); }; } and in the implementation file: // C.cpp #include "C.h" using namespace NS; void C::f() { //... } All…
13
votes
3 answers

Where to put using directives in C++ header files

For my project I am using some pretty convoluted data structures, e.g. std::unordered_map>> for which I would like to declare type aliases for readability. The code on which I built my project on already…
Hooloovoo
  • 133
  • 6
13
votes
2 answers

Have ReSharper keep 'using System;' when optimizing usings

I was wondering if there is some option to keep ReSharper from removing just the using System; directive? Perhaps this is configurable somewhere? Also, is there a way to have ReSharper sort the remaining directives just as Visual Studio 2008 does it…
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
13
votes
1 answer

Apply a 'using std::foo' directive to a constructor initializer list locally (C++)

Given a custom type, the following fragment shows the common approach for allowing a function to automatically select a user provided overload specific to the type, or a generic implementation of a function from the standard library if not. //…
marack
  • 2,024
  • 22
  • 31
13
votes
2 answers

friend class declaration and using directive

Is the following example well-formed? namespace N { class A; } using namespace N; class B { int i; friend class A; }; namespace N { class A { B m; int get() { return m.i; } }; } This example compiled…
Mitsuru Kariya
  • 938
  • 4
  • 14
13
votes
1 answer

static constexpr member function in templated using expression not found

For the following code #include template struct kernel { static constexpr unsigned max_pole(unsigned P) { return P>MaxP? MaxP:P; } template using array = std::array; …
Walter
  • 44,150
  • 20
  • 113
  • 196
1
2
3
19 20