specializing std::less

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Exits Funnel

    #1

    specializing std::less

    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_p air_t> { };
    //End foo.cpp

    g++ has this to say on the issue:

    foo.cpp:7: specializing `struct std::less<int_p air_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

  • Victor Bazarov

    #2
    Re: specializing std::less

    "Exits Funnel" <exitsNOfunnelS PAM@yahoo.com> wrote...[color=blue]
    > [...] 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.[/color]

    It's expressly allowed by the Standard.

    Victor


    Comment

    • Sharad Kala

      #3
      Re: specializing std::less


      "Exits Funnel" <exitsNOfunnelS PAM@yahoo.com> wrote in message[color=blue]
      > I've got this code:
      >[/color]
      [snip]
      [color=blue]
      > 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.[/color]

      That's fine (Read 17.4.3.1/1)

      Sharad




      Comment

      • Dietmar Kuehl

        #4
        Re: specializing std::less


        Victor Bazarov wrote:[color=blue]
        > "Exits Funnel" <exitsNOfunnelS PAM@yahoo.com> wrote...[color=green]
        > > [...] I can fix the problem by wrapping the specialization in a
        > > 'namespace std' block but I'm wondering if this is prudent or is[/color][/color]
        there[color=blue][color=green]
        > > some other solution. Thanks.[/color]
        >
        > It's expressly allowed by the Standard.[/color]

        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:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
        Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.co m/>

        Comment

        • Sharad Kala

          #5
          Re: specializing std::less


          "Dietmar Kuehl" <dietmar_kuehl@ yahoo.com> wrote in message
          [color=blue]
          >
          > Victor Bazarov wrote:[color=green]
          > > "Exits Funnel" <exitsNOfunnelS PAM@yahoo.com> wrote...[color=darkred]
          > > > [...] I can fix the problem by wrapping the specialization in a
          > > > 'namespace std' block but I'm wondering if this is prudent or is[/color][/color]
          > there[color=green][color=darkred]
          > > > some other solution. Thanks.[/color]
          > >
          > > It's expressly allowed by the Standard.[/color]
          >
          > 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![/color]

          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


          Comment

          • Exits Funnel

            #6
            Re: specializing std::less

            Sharad Kala wrote:[color=blue]
            > "Dietmar Kuehl" <dietmar_kuehl@ yahoo.com> wrote in message
            >
            >[color=green]
            >>Victor Bazarov wrote:
            >>[color=darkred]
            >>>"Exits Funnel" <exitsNOfunnelS PAM@yahoo.com> wrote...
            >>>
            >>>>[...] I can fix the problem by wrapping the specialization in a
            >>>>'namespac e std' block but I'm wondering if this is prudent or is[/color]
            >>
            >>there
            >>[color=darkred]
            >>>>some other solution. Thanks.
            >>>
            >>>It's expressly allowed by the Standard.[/color]
            >>
            >>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![/color]
            >
            >
            > 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
            >
            >[/color]

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

            -exits

            Comment

            Working...