specializing std::less

E

Exits Funnel

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
 
V

Victor Bazarov

Exits Funnel said:
[...] 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
 
S

Sharad Kala

Exits Funnel said:
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
 
D

Dietmar Kuehl

Victor said:
Exits Funnel said:
[...] 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...
 
S

Sharad Kala

Dietmar Kuehl said:
Victor said:
Exits Funnel said:
[...] 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
 
E

Exits Funnel

Sharad said:
Victor said:
[...] 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
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top