From -1953887343828382897
X-Google-Thread: 7894ca11fe,421cd8b98acf7c5
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!news.glorb.com!news2.glorb.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: frege <gottlobfrege@gmail.com>
Newsgroups: comp.std.c++
Subject: Re: Why there are both complex::real and std::real?
Date: Tue,  3 Nov 2009 12:26:59 CST
Organization: http://groups.google.com
Lines: 64
Sender: cppmods@cs.rpi.edu
Approved: stephen.clamage@sun.com
Message-ID: <41c3b2ca-7cbe-4637-afd1-b78e1ae0c74d@j4g2000yqe.googlegroups.com>
References: <210ca399-bea2-47bf-b614-f4cef8fe25fd@k26g2000vbp.googlegroups.com>
  <ab16ddac-9dbf-4d57-9c9e-bdfddb9839ed@12g2000pri.googlegroups.com>
  <9f639ca8-87a5-43fe-923f-c62a4bea7cfa@j4g2000yqa.googlegroups.com>
  <aa2d0be1-e9fd-431d-a495-a5adc2b947ed@h40g2000prf.googlegroups.com>
  <hce2pq$mgn$1@news.mixmin.net>
  <1858197e-a472-4036-8716-bf96f5a0bb34@k4g2000yqb.googlegroups.com>
  <hckcm4$9h2$1@news.mixmin.net>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain; charset=ISO-8859-1
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Mon, 2 Nov 2009 10:11:49 -0800 (PST)
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:1644

On Nov 1, 10:26 pm, "Joe Smith" <unknown_kev_...@hotmail.com> wrote:
>
> I also have some namespacing concerns with free functions. Free functions
> can be used in two different ways. I'll show these by example.
>
> namespace complex_utils{
> template<typename T>
> T conj(T& t)
> { return T(std::real(t), -std::imag(t)); }
>
> }
>
> That sort of does not cause namespace issues. When I define my new
> complex-like type that is comaptible enough that many templates designed for
> std::complex can use it, I simply inject a new overload into namespace std,
> which I am explicitly permitted to do.
>
> The other form is as follows.
>
> namespace soemthing{
> template<typename T>
> T conj(const T& t)
> { return T(real(t), -imag(t)); }
>
> }
>
> Here, ADL is being used. Injecting an overload into namespace std will do
> nothing here, since the compiler will be looking in my namespace for real
> and imag. Thus in some sense, now I need a my_ns::real(my_complex) function,
> when it is conceptually an overload of std::real, and thus ought to belong
> there. In some sense then my namespace is being infringed upon. I would not
> be objecting to the infringment upon the ::my_ns::my_complex namespace of
> member functions, since my goal is compaibilty, but infringing the ::my_ns
> namespace so that ::my_ns::my_complex can be compatible seems wrong.
>

template<typename T>
T conj(const T& t)
{
    using namespace std;

    return T(real(t), -imag(t));
}

I've seen this form a few times (if I recall it correctly),
particularly for swap() which is sometimes injected into std::,
sometimes not, depending on implementor.
I think this then catches both cases, favouring the T version of real
()/imag() if it exists (via ADL).

I agree with you that, in general, the right thing to do is to
overload std::real(), as that is the context of what you are
implementing.  Otherwise a function like real(t) could be anything -
not related to complex numbers at all...

Tony


-- 
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



