Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 first two:

using namespace System;
namespace Diag = System::Diagnostics;

Is there any way to do the third one in C++/CLI?

Doing namespace DBG = System::Diagnostics::Debug; gives error C2879: 'System::Diagnostics::Debug' : only an existing namespace can be given an alternative name by a namespace alias definition

The only alterntive I've come up with is #define DBG System::Diagnostics::Debug, but I'd prefer a proper using directive, if available.

like image 425
David Yaw Avatar asked Dec 01 '10 19:12

David Yaw


People also ask

How do you namespace an alias?

A namespace can be given an alias (i.e., another name for the same namespace) using the namespace identifier = syntax. Members of the aliased namespace can be accessed by qualifying them with the name of the alias.

Where and how it is alias used C#?

Fortunately, C# provides a way to alias a long namespace or class name while retaining full type-checking. To alias a namespace or class name, use the using directive to define the alias as shown in the sample code below. Then you can use the alias anywhere you would normally use the class name or namespace.

What is an alias in C#?

Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace for the sake of clarity. At the heart of these features is the using keyword.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.


1 Answers

A C++ typedef will do the trick here.

typedef System::Diagnostics::Debug DBG;
like image 52
JaredPar Avatar answered Sep 28 '22 03:09

JaredPar