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

specializing std::less

P: n/a
I've got this code:

//Begin foo.cpp
#include <utility>
#include <bits/stl_function.h>

typedef std::pair<int, int> int_pair_t;

template<>
struct std::less<int_pair_t> { };
//End foo.cpp

g++ has this to say on the issue:

foo.cpp:7: specializing `struct std::less<int_pair_t>' in different
namespace
/usr/include/c++/3.2.3/bits/stl_function.h:195: from definition of `
template<class _Tp> struct std::less'

This seems reasonable (though for what it's worth, it compiles under
VC7.1). I can fix the problem by wrapping the specialization in a
'namespace std' block but I'm wondering if this is prudent or is there
some other solution. Thanks.

-exits

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Exits Funnel" <ex***************@yahoo.com> wrote...
[...] I can fix the problem by wrapping the specialization in a
'namespace std' block but I'm wondering if this is prudent or is there
some other solution. Thanks.


It's expressly allowed by the Standard.

Victor
Jul 22 '05 #2

P: n/a

"Exits Funnel" <ex***************@yahoo.com> wrote in message
I've got this code:
[snip]
This seems reasonable (though for what it's worth, it compiles under
VC7.1). I can fix the problem by wrapping the specialization in a
'namespace std' block but I'm wondering if this is prudent or is there
some other solution. Thanks.


That's fine (Read 17.4.3.1/1)

Sharad


Jul 22 '05 #3

P: n/a

Victor Bazarov wrote:
"Exits Funnel" <ex***************@yahoo.com> wrote...
[...] I can fix the problem by wrapping the specialization in a
'namespace std' block but I'm wondering if this is prudent or is there some other solution. Thanks.


It's expressly allowed by the Standard.


It is allowed to [partially] specialize class templates from the
standard library in namespace 'std' *IF* the specialization involves
a user defined type. 'std::pair<int, int>' as used in the original
article does not qualify for a user defined specialization!

That said, it is probably save to specialize things which don't exist
according to the standard - only there is no guarantee that it indeed
works...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

Jul 22 '05 #4

P: n/a

"Dietmar Kuehl" <di***********@yahoo.com> wrote in message

Victor Bazarov wrote:
"Exits Funnel" <ex***************@yahoo.com> wrote...
[...] I can fix the problem by wrapping the specialization in a
'namespace std' block but I'm wondering if this is prudent or is there some other solution. Thanks.


It's expressly allowed by the Standard.


It is allowed to [partially] specialize class templates from the
standard library in namespace 'std' *IF* the specialization involves
a user defined type. 'std::pair<int, int>' as used in the original
article does not qualify for a user defined specialization!


True, but this is a well known work around that works on all the modern
compilers I have tested it on. But as you say it is not legal as quoted in
the Standard. Is there a proposal to change it (for std::pair) ?

Sharad
Jul 22 '05 #5

P: n/a
Sharad Kala wrote:
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message

Victor Bazarov wrote:
"Exits Funnel" <ex***************@yahoo.com> wrote...

[...] I can fix the problem by wrapping the specialization in a
'namespace std' block but I'm wondering if this is prudent or is


there
some other solution. Thanks.

It's expressly allowed by the Standard.


It is allowed to [partially] specialize class templates from the
standard library in namespace 'std' *IF* the specialization involves
a user defined type. 'std::pair<int, int>' as used in the original
article does not qualify for a user defined specialization!

True, but this is a well known work around that works on all the modern
compilers I have tested it on. But as you say it is not legal as quoted in
the Standard. Is there a proposal to change it (for std::pair) ?

Sharad


Thanks Victor, Sharad, Dietmar. I appreciate the information.

-exits

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.