what's the point of namespace aliases?

C

Chris Thomasson

I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:


__________________________
#include <stdio.h>


// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}
}


// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}
}


// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;


// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;
}


// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};
}
*/


// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};
}


// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;
}

_______________________


Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...
 
P

Pierre Senellart

"Chris Thomasson" ,comp.lang.c++:
Anybody making good use out of namespace aliases?

Well, you can just use them for abbreviations; for instance, when using
Boost MPL library, it's quite common to write:

namespace mpl=boost:mpl;
 
C

Chris Thomasson

Pierre Senellart said:
"Chris Thomasson" ,comp.lang.c++:


Well, you can just use them for abbreviations; for instance, when using
Boost MPL library, it's quite common to write:

namespace mpl=boost:mpl;

what the difference between that and this:

namespace mpl {
using namespace boost::mpl;
}

?

I really don't see the main point of namespace aliases when we already have
the more flexible, IMHO, 'using namespace' keyword...
 
P

Pierre Senellart

"Chris Thomasson" ,comp.lang.c++:
I really don't see the main point of namespace aliases when we already have
the more flexible, IMHO, 'using namespace' keyword...

I guess it is less powerful in the sense that you cannot add or override
things, but sometimes you just want to have an alias, nothing more, and
the namespace alias syntax is perfect for this role...

You could also argue in a similar way, that there is never a reason for
doing this :

typedef std::map<std::string,std::string> map;

because you have a more powerful way to do this by using inheritance:

class map : public std::map<std::string,std::string> {};

But here, the typedef is more appropriate because it makes the intent of
the programmer clear.
 
S

Sarath

I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:

__________________________
#include <stdio.h>

// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}

}

// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}

}

// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;

// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;

}

// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};}

*/

// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};

}

// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;

}

_______________________

Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...

As all of you, I used namespaces as it is. Never used aliases.
But in Effective C++ 3rd Edition, in the introduction section of C++
0x, Scott Meyers really explains the use of namespace aliases.

e.g In the case of C++ 0x, some of the TR1 components are taken from
Boost library. As the standard still under development and no compiler
available to test the new functionality of C++ 0x (heard that GNU C++
implemented some features of the proposed standard), we can't do some
samples and experiments. The components will be defined un TR1
namespace so that we can use componenents of boost namespaces as they
are in TR1 namespace. But still a question is really used by
somebody :_
 
C

coosa

I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:

__________________________
#include <stdio.h>

// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}

}

// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}

}

// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;

// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;

}

// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};}

*/

// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};

}

// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;

}

_______________________

Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...

Namespaces are not only essential for me in C++, but also under
the .Net Framework; it simply makes my code readable and manages any
clashes of same names!!
Consider this header file:

#ifndef converterHPP
#define converterHPP

#include <sstream>

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (const std::string& str)
{
T num;
std::istringstream str_s (str);
str_s >> num;
return num;
}
};

class number
{
public:
template <typename T>
static std::string to_string (const T& num)
{
std::eek:stringstream o_str;
o_str << num;
return o_str.str();
}
};
}

#endif

It's up to me to name my namespace "convert", but I called one class
here "string" and it already exists in the standard c++ string
library!
In my code to execute, I could make the default namespace std and:

double d = 12342354.76658;
string str = convert::string<double>(d);

std::string versus convert::string; same class under two different
namespaces; it would have been a clash otherwise!
Don't you still see a use of this wonderful namespacing feature?!
 
C

coosa

I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:

__________________________
#include <stdio.h>

// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}

}

// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}

}

// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;

// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;

}

// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};}

*/

// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};

}

// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;

}

_______________________

Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...

Namespaces are not only essential for me in C++, but also under
the .Net Framework; it simply makes my code readable and manages any
clashes of same names!!
Consider this header file:

#ifndef converterHPP
#define converterHPP

#include <sstream>

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (const std::string& str)
{
T num;
std::istringstream str_s (str);
str_s >> num;
return num;
}
};

class number
{
public:
template <typename T>
static std::string to_string (const T& num)
{
std::eek:stringstream o_str;
o_str << num;
return o_str.str();
}
};

}

#endif

It's up to me to name my namespace "convert", but I called one class
here "string" and it already exists in the standard c++ string
library!
In my code to execute, I could make the default namespace std and:

double d = 12342354.76658;
string str = convert::string<double>::to_number(d);

std::string versus convert::string; same class under two different
namespaces; it would have been a clash otherwise!
Don't you still see a use of this wonderful namespacing feature?!
 
C

coosa

I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:

__________________________
#include <stdio.h>

// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}

}

// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}

}

// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;

// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;

}

// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};}

*/

// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};

}

// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;

}

_______________________

Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...

Namespaces are not only essential for me in C++, but also under
the .Net Framework; it simply makes my code readable and manages any
clashes of same names!!
Consider this header file:

#ifndef converterHPP
#define converterHPP

#include <sstream>

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (const std::string& str)
{
T num;
std::istringstream str_s (str);
str_s >> num;
return num;
}
};

class number
{
public:
template <typename T>
static std::string to_string (const T& num)
{
std::eek:stringstream o_str;
o_str << num;
return o_str.str();
}
};

}

#endif

It's up to me to name my namespace "convert", but I called one class
here "string" and it already exists in the standard c++ string
library!
In my code to execute, I could make the default namespace std and:

string str = "12342354.76658";
double d = convert::string<double>::to_number(str);

std::string versus convert::string; same class under two different
namespaces; it would have been a clash otherwise!
Don't you still see a use of this wonderful namespacing feature?!
 
A

Adrian Hawryluk

coosa said:
I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:

__________________________
#include <stdio.h>

// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}

}

// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}

}

// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;

// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;

}

// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};}

*/

// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};

}

// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;

}

_______________________

Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...

Namespaces are not only essential for me in C++, but also under
the .Net Framework; it simply makes my code readable and manages any
clashes of same names!!
Consider this header file:

#ifndef converterHPP
#define converterHPP

#include <sstream>

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (const std::string& str)
{
T num;
std::istringstream str_s (str);
str_s >> num;
return num;
}
};

class number
{
public:
template <typename T>
static std::string to_string (const T& num)
{
std::eek:stringstream o_str;
o_str << num;
return o_str.str();
}
};

}

#endif

It's up to me to name my namespace "convert", but I called one class
here "string" and it already exists in the standard c++ string
library!
In my code to execute, I could make the default namespace std and:

string str = "12342354.76658";
double d = convert::string<double>::to_number(str);

std::string versus convert::string; same class under two different
namespaces; it would have been a clash otherwise!
Don't you still see a use of this wonderful namespacing feature?!
He is talking about namespace aliases, not namespaces.

Also, why did you post 3 times the same message?


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/by-nc-sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
 
C

coosa

coosa said:
I was thinking of how I was going to create a robust versioning system in
Standard C++ for my library and was wondering exactly what the point of a
namespace alias is? The seem like a rather limited approach. For instance,
you simply cannot "add-on" to a aliased namespace... Here is a quick example
of what I am talking about:
__________________________
#include <stdio.h>
// version 1 of my library
namespace mylib_version_1 {
namespace constant {
char const name[] = "My Library Version 1.0";
}
}
// version 1 of my library
namespace mylib_version_2 {
namespace constant {
char const name[] = "My Library Version 2.0";
}
}
// method 1: create namespace alias to version 1
namespace mylib_alias = mylib_version_1;
// method 2: create namespace using version 2
namespace mylib_using {
using namespace mylib_version_2;
}
// can't add to namespace mylib_alias!!
// uncomment for error!
// why can't you do this???
/*
namespace mylib_alias {
class something_extra {
};}
*/
// add to namespace mylib_using
namespace mylib_using {
class something_extra {
};
}
// entry
int main() {
printf("method 1: %s\n", mylib_alias::constant::name);
printf("method 2: %s\n", mylib_using::constant::name);
return 0;
}
_______________________
Anybody making good use out of namespace aliases? I can't think of why I
would choose to use 'method 1' over 'method 2' in the implementation of my
versioning system. Especially when you can't add to a namespace alias
created with 'method 1'! I know I must be missing something here... Humm...
Namespaces are not only essential for me in C++, but also under
the .Net Framework; it simply makes my code readable and manages any
clashes of same names!!
Consider this header file:
#ifndef converterHPP
#define converterHPP
#include <sstream>
namespace convert
{
class string
{
public:
template <typename T>
static T to_number (const std::string& str)
{
T num;
std::istringstream str_s (str);
str_s >> num;
return num;
}
};
class number
{
public:
template <typename T>
static std::string to_string (const T& num)
{
std::eek:stringstream o_str;
o_str << num;
return o_str.str();
}
};


It's up to me to name my namespace "convert", but I called one class
here "string" and it already exists in the standard c++ string
library!
In my code to execute, I could make the default namespace std and:
string str = "12342354.76658";
double d = convert::string<double>::to_number(str);
std::string versus convert::string; same class under two different
namespaces; it would have been a clash otherwise!
Don't you still see a use of this wonderful namespacing feature?!

He is talking about namespace aliases, not namespaces.

Also, why did you post 3 times the same message?

Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/by-nc-sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/

I believe I deleted the previous two; sorry for the inconvenience
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

"Chris Thomasson" ,comp.lang.c++:
class map : public std::map<std::string,std::string> {};

I was under the impression that you didn't get the constructors if you
did this? Do you not have to declare constructors and forward them to
the superclass or am I confused?


K
 
P

Pierre Senellart

Kirit Sælensminde:
I was under the impression that you didn't get the constructors if you
did this? Do you not have to declare constructors and forward them to
the superclass or am I confused?

Sure, unless you only need default and copy constructors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top