By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,121 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,121 IT Pros & Developers. It's quick & easy.

what's the point of namespace aliases?

P: n/a
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...
Mar 13 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
"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;
Mar 13 '07 #2

P: n/a
"Pierre Senellart" <in*****@invalid.invalidwrote in message
news:et***********@nef.ens.fr...
"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;
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...
Mar 13 '07 #3

P: n/a
"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::stringmap;

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.
Mar 13 '07 #4

P: n/a
On Mar 13, 6:54 pm, "Chris Thomasson" <cris...@comcast.netwrote:
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 :_

Mar 13 '07 #5

P: n/a
On Mar 13, 5:54 pm, "Chris Thomasson" <cris...@comcast.netwrote:
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::ostringstream 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?!

Mar 14 '07 #6

P: n/a
On Mar 13, 5:54 pm, "Chris Thomasson" <cris...@comcast.netwrote:
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::ostringstream 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?!

Mar 14 '07 #7

P: n/a
On Mar 13, 5:54 pm, "Chris Thomasson" <cris...@comcast.netwrote:
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::ostringstream 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?!

Mar 14 '07 #8

P: n/a
coosa wrote:
On Mar 13, 5:54 pm, "Chris Thomasson" <cris...@comcast.netwrote:
>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::ostringstream 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/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 17 '07 #9

P: n/a
On Mar 17, 11:57 pm, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
coosa wrote:
On Mar 13, 5:54 pm, "Chris Thomasson" <cris...@comcast.netwrote:
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::ostringstream 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/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
I believe I deleted the previous two; sorry for the inconvenience

Mar 18 '07 #10

P: n/a
On Mar 13, 6:28 pm, Pierre Senellart <inva...@invalid.invalidwrote:
"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
Mar 19 '07 #11

P: n/a
Kirit Sælensminde:
>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?
Sure, unless you only need default and copy constructors.
Mar 19 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.