Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11583 > unrolled thread

namespace and global function

Started byJayden Shui <jayden.shui@gmail.com>
First post2011-11-17 14:09 -0800
Last post2011-11-18 13:17 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  namespace and global function Jayden Shui <jayden.shui@gmail.com> - 2011-11-17 14:09 -0800
    Re: namespace and global function Joshua Maurice <joshuamaurice@gmail.com> - 2011-11-17 15:29 -0800
    Re: namespace and global function SG <s.gesemann@gmail.com> - 2011-11-18 01:00 -0800
    Re: namespace and global function "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-18 13:17 +0100

#11583 — namespace and global function

FromJayden Shui <jayden.shui@gmail.com>
Date2011-11-17 14:09 -0800
Subjectnamespace and global function
Message-ID<1997973f-a426-4f32-9a58-6195c424f1d3@hh9g2000vbb.googlegroups.com>
Hello All,

I have a global function to clone an object.

template<class T>
T* Clone(T const& t)
{
    return new T(t);
}

and I have a class A in a namespace S which needs a specialized Clone
function

namespace S {

class A
{
    friend A* Clone(A const& a)
    {
        // do something special
        return new A(a);
    }
};

};

The clone function for S::A is introduced in the namespace S, that is
S::Clone. It is not in the global scope. Is there any way to introduce
it in the global scope while still keeping the function body in the
namespace scope.

Thank you very much for your kind help!

Best regards,

Jayden

[toc] | [next] | [standalone]


#11587

FromJoshua Maurice <joshuamaurice@gmail.com>
Date2011-11-17 15:29 -0800
Message-ID<6c293989-aeb2-4a23-a90d-b55dc9152710@k10g2000yqn.googlegroups.com>
In reply to#11583
On Nov 17, 2:09 pm, Jayden Shui <jayden.s...@gmail.com> wrote:
> Hello All,
>
> I have a global function to clone an object.
>
> template<class T>
> T* Clone(T const& t)
> {
>     return new T(t);
>
> }
>
> and I have a class A in a namespace S which needs a specialized Clone
> function
>
> namespace S {
>
> class A
> {
>     friend A* Clone(A const& a)
>     {
>         // do something special
>         return new A(a);
>     }
>
> };
> };
>
> The clone function for S::A is introduced in the namespace S, that is
> S::Clone. It is not in the global scope. Is there any way to introduce
> it in the global scope while still keeping the function body in the
> namespace scope.

Yes. A using declaration in global scope.
    using S::Clone;

Also, IIRC a friend declaration in a class doesn't inject the name
into the surrounding scope, so that using declaration will fail.
Instead, you need the following (at global scope):
    namespace S { A* Clone(A const& a); }
    using S::Clone;

I'd suggest googling Argument Dependent Lookup (ADL) aka Koenig
lookup, as that was meant to solve this problem.

[toc] | [prev] | [next] | [standalone]


#11591

FromSG <s.gesemann@gmail.com>
Date2011-11-18 01:00 -0800
Message-ID<7eab36fd-ab4f-4d9c-a2e3-d2956fe81a74@p1g2000yqh.googlegroups.com>
In reply to#11583
On Nov 17, 11:09 pm, Jayden Shui wrote:
>
> I have a global function to clone an object.
>
>   template<class T>
>   T* Clone(T const& t)
>   {
>       return new T(t);
>   }
>
> and I have a class A in a namespace S which needs a specialized Clone
> function
>
> namespace S {
>
> class A
> {
>     friend A* Clone(A const& a)
>     {
>         // do something special
>         return new A(a);
>     }
> };
> }
>
> The clone function for S::A is introduced in the namespace S,
> that is S::Clone.

That's not true. Clone is not injected into the namespace S. But it
can be found via ADL (argument dependent lookup).

> It is not in the global scope. Is there any way to introduce
> it in the global scope while still keeping the function body in the
> namespace scope.

Why? Isn't ADL not enough for you? Just use "Clone" unqualified. If
you want to fall back on your global template you can do so with a
using declaration just to be safe

  namespace funky {

    template<class T>
    class indirect_value {
      T* ptr_;
    public:
      :::

      indirect_value(indirect_value const& x)
      : ptr_(0) {
        using ::Clone; // to avoid hiding issues with other, unrelated
Clone functions
        ptr_ = Clone(*x.ptr_); // ADL is applicable, falls back
on ::Clone
      }

      :::
    };

  }

You should understand how unqualified name lookup works.

Cheers!
SG

[toc] | [prev] | [next] | [standalone]


#11598

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2011-11-18 13:17 +0100
Message-ID<ja5idf$9rl$1@dont-email.me>
In reply to#11583
On 17.11.2011 23:09, Jayden Shui wrote:
>
> I have a global function to clone an object.
>
> template<class T>
> T* Clone(T const&  t)
> {
>      return new T(t);
> }

This will use the statically known type, so if T is polymorphic it may 
not necessarily clone in the conventional meaning of the word as 
creating a copy.

Rather, when the dynamic type of `t` is a subclass of T, you'll get a slice.

That's why 'clone' is usually a virtual member function.


Cheers & hth.,

- Alf

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web