adding function overload to std - Good or Bad?

Discussion in 'C++' started by Daniel Pitts, Jun 7, 2008.

  1. Daniel Pitts

    Daniel Pitts Guest

    I've created a class with semantics such that it makes sense to call
    std::abs(foo) and get a (double) result.

    Is it sensible to add to the std namespace, or would it be better to put
    an "abs" function it in the same namespace as my class was created?


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 7, 2008
    #1
    1. Advertising

  2. Daniel Pitts

    sumsin Guest

    On Jun 7, 6:48 am, Daniel Pitts
    <> wrote:
    > I've created a class with semantics such that it makes sense to call
    > std::abs(foo) and get a (double) result.
    >
    > Is it sensible to add to the std namespace, or would it be better to put
    > an "abs" function it in the same namespace as my class was created?
    >
    > --
    > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


    No it wouldn't be good.
    sumsin, Jun 7, 2008
    #2
    1. Advertising

  3. Daniel Pitts

    Daniel Pitts Guest

    sumsin wrote:
    > On Jun 7, 6:48 am, Daniel Pitts
    > <> wrote:
    >> I've created a class with semantics such that it makes sense to call
    >> std::abs(foo) and get a (double) result.
    >>
    >> Is it sensible to add to the std namespace, or would it be better to put
    >> an "abs" function it in the same namespace as my class was created?
    >>
    >> --
    >> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

    >
    > No it wouldn't be good.

    Thanks for the ambiguous response, it's very helpful.

    Which wouldn't be good, and more importantly, *why*?

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 7, 2008
    #3
  4. Daniel Pitts

    sumsin Guest

    On Jun 7, 11:00 am, Daniel Pitts
    <> wrote:
    > sumsin wrote:
    > > On Jun 7, 6:48 am, Daniel Pitts
    > > <> wrote:
    > >> I've created a class with semantics such that it makes sense to call
    > >> std::abs(foo) and get a (double) result.

    >
    > >> Is it sensible to add to the std namespace, or would it be better to put
    > >> an "abs" function it in the same namespace as my class was created?

    >
    > >> --
    > >> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

    >
    > > No it wouldn't be good.

    >
    > Thanks for the ambiguous response, it's very helpful.
    >
    > Which wouldn't be good, and more importantly, *why*?
    >
    > --
    > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


    because std namespace is meant for standard utilities and your
    semantics for 'abs' function is not standard from the language
    perspective thats why i am suggesting no. So keep it into your custom
    namespace.
    sumsin, Jun 7, 2008
    #4
  5. Daniel Pitts

    Rolf Magnus Guest

    Daniel Pitts wrote:

    > sumsin wrote:
    >> On Jun 7, 6:48 am, Daniel Pitts
    >> <> wrote:
    >>> I've created a class with semantics such that it makes sense to call
    >>> std::abs(foo) and get a (double) result.
    >>>
    >>> Is it sensible to add to the std namespace, or would it be better to put
    >>> an "abs" function it in the same namespace as my class was created?
    >>>
    >>> --
    >>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

    >>
    >> No it wouldn't be good.

    > Thanks for the ambiguous response, it's very helpful.
    >
    > Which wouldn't be good,


    Putting it into namespace std.

    > and more importantly, *why*?


    One reason would be because the C++ standard forbids it.
    Rolf Magnus, Jun 7, 2008
    #5
  6. On Jun 7, 7:10 am, sumsin <> wrote:
    > On Jun 7, 11:00 am, Daniel Pitts
    >
    >
    >
    >
    >
    > <> wrote:
    > > sumsin wrote:
    > > > On Jun 7, 6:48 am, Daniel Pitts
    > > > <> wrote:
    > > >> I've created a class with semantics such that it makes sense to call
    > > >> std::abs(foo) and get a (double) result.

    >
    > > >> Is it sensible to add to the std namespace, or would it be better to put
    > > >> an "abs" function it in the same namespace as my class was created?

    >
    > > >> --
    > > >> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

    >
    > > > No it wouldn't be good.

    >
    > > Thanks for the ambiguous response, it's very helpful.

    >
    > > Which wouldn't be good, and more importantly, *why*?

    >
    > > --
    > > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

    >
    > because std namespace is meant for standard utilities and your
    > semantics for 'abs' function is not standard from the language
    > perspective thats why i am suggesting no. So keep it into your custom
    > namespace.


    Or you could do:

    namespace super_std {

    using namespace std;

    /* Put more stuff here */
    }
    Tomás Ó hÉilidhe, Jun 7, 2008
    #6
  7. Daniel Pitts

    red floyd Guest

    Rolf Magnus wrote:
    > Daniel Pitts wrote:
    >
    >> sumsin wrote:
    >>> On Jun 7, 6:48 am, Daniel Pitts
    >>> <> wrote:
    >>>> I've created a class with semantics such that it makes sense to call
    >>>> std::abs(foo) and get a (double) result.
    >>>>
    >>>> Is it sensible to add to the std namespace, or would it be better to put
    >>>> an "abs" function it in the same namespace as my class was created?
    >>>>
    >>>> --
    >>>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    >>> No it wouldn't be good.

    >> Thanks for the ambiguous response, it's very helpful.
    >>
    >> Which wouldn't be good,

    >
    > Putting it into namespace std.
    >
    >> and more importantly, *why*?

    >
    > One reason would be because the C++ standard forbids it.
    >


    I thought it was OK to add template specializations in std::
    red floyd, Jun 8, 2008
    #7
  8. Daniel Pitts

    Rolf Magnus Guest

    red floyd wrote:

    > Rolf Magnus wrote:
    >> Daniel Pitts wrote:
    >>
    >>> sumsin wrote:
    >>>> On Jun 7, 6:48 am, Daniel Pitts
    >>>> <> wrote:
    >>>>> I've created a class with semantics such that it makes sense to call
    >>>>> std::abs(foo) and get a (double) result.
    >>>>>
    >>>>> Is it sensible to add to the std namespace, or would it be better to
    >>>>> put an "abs" function it in the same namespace as my class was
    >>>>> created?
    >>>>>
    >>>>> --
    >>>>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    >>>> No it wouldn't be good.
    >>> Thanks for the ambiguous response, it's very helpful.
    >>>
    >>> Which wouldn't be good,

    >>
    >> Putting it into namespace std.
    >>
    >>> and more importantly, *why*?

    >>
    >> One reason would be because the C++ standard forbids it.
    >>

    >
    > I thought it was OK to add template specializations in std::


    Yes, but the OP wants to add an overload, not a template specialization.
    Rolf Magnus, Jun 8, 2008
    #8
  9. Daniel Pitts

    red floyd Guest

    Rolf Magnus wrote:
    > red floyd wrote:
    >
    >> Rolf Magnus wrote:
    >>> Daniel Pitts wrote:
    >>>
    >>>> sumsin wrote:
    >>>>> On Jun 7, 6:48 am, Daniel Pitts
    >>>>> <> wrote:
    >>>>>> I've created a class with semantics such that it makes sense to call
    >>>>>> std::abs(foo) and get a (double) result.
    >>>>>>
    >>>>>> Is it sensible to add to the std namespace, or would it be better to
    >>>>>> put an "abs" function it in the same namespace as my class was
    >>>>>> created?
    >>>>>>
    >>>>>> --
    >>>>>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    >>>>> No it wouldn't be good.
    >>>> Thanks for the ambiguous response, it's very helpful.
    >>>>
    >>>> Which wouldn't be good,
    >>> Putting it into namespace std.
    >>>
    >>>> and more importantly, *why*?
    >>> One reason would be because the C++ standard forbids it.
    >>>

    >> I thought it was OK to add template specializations in std::

    >
    > Yes, but the OP wants to add an overload, not a template specialization.
    >


    Isn't std::abs a template?
    red floyd, Jun 8, 2008
    #9
  10. Daniel Pitts

    Rolf Magnus Guest

    red floyd wrote:


    >>>>> Which wouldn't be good,
    >>>> Putting it into namespace std.
    >>>>
    >>>>> and more importantly, *why*?
    >>>> One reason would be because the C++ standard forbids it.
    >>>>
    >>> I thought it was OK to add template specializations in std::

    >>
    >> Yes, but the OP wants to add an overload, not a template specialization.
    >>

    >
    > Isn't std::abs a template?


    Well, there is an std::abs template, but only for std::valarray:

    template<class T> valarray<T> abs (const valarray<T>&);

    The rest are just overloads.
    Rolf Magnus, Jun 8, 2008
    #10
    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. Piotre Ugrumov
    Replies:
    3
    Views:
    368
    Nick Hounsome
    Jan 25, 2004
  2. rantingrick
    Replies:
    44
    Views:
    1,199
    Peter Pearson
    Jul 13, 2010
  3. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
  4. Jeremy Murphy
    Replies:
    5
    Views:
    473
    Jeremy Murphy
    Dec 13, 2012
  5. Shriramana Sharma
    Replies:
    2
    Views:
    149
    Shriramana Sharma
    Jun 27, 2013
Loading...

Share This Page