specializing std::less

Discussion in 'C++' started by Exits Funnel, Jan 12, 2005.

  1. Exits Funnel

    Exits Funnel Guest

    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
     
    Exits Funnel, Jan 12, 2005
    #1
    1. Advertising

  2. "Exits Funnel" <> 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
     
    Victor Bazarov, Jan 12, 2005
    #2
    1. Advertising

  3. Exits Funnel

    Sharad Kala Guest

    "Exits Funnel" <> 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
     
    Sharad Kala, Jan 12, 2005
    #3
  4. Victor Bazarov wrote:
    > "Exits Funnel" <> 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:> <http://www.dietmar-kuehl.de/>
    Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
     
    Dietmar Kuehl, Jan 12, 2005
    #4
  5. Exits Funnel

    Sharad Kala Guest

    "Dietmar Kuehl" <> wrote in message

    >
    > Victor Bazarov wrote:
    > > "Exits Funnel" <> 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
     
    Sharad Kala, Jan 12, 2005
    #5
  6. Exits Funnel

    Exits Funnel Guest

    Sharad Kala wrote:
    > "Dietmar Kuehl" <> wrote in message
    >
    >
    >>Victor Bazarov wrote:
    >>
    >>>"Exits Funnel" <> 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
     
    Exits Funnel, Jan 12, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinu
    Replies:
    4
    Views:
    360
    Jim Langston
    Jul 7, 2005
  2. Vinu
    Replies:
    0
    Views:
    356
  3. Cory Nelson
    Replies:
    16
    Views:
    1,162
    red floyd
    Aug 4, 2006
  4. jiajia wu
    Replies:
    0
    Views:
    358
    jiajia wu
    Oct 1, 2009
  5. 6668
    Replies:
    0
    Views:
    150
Loading...

Share This Page