PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Pimpl idiom without dynamic memory allocation
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Pimpl idiom without dynamic memory allocation

Réponse
 
LinkBack Outils de la discussion
Vieux 17/10/2007, 19h29   #1 (permalink)
Daniel Lidström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Pimpl idiom without dynamic memory allocation

Hello!

I have just discovered a way to use the private implementation idiom
(pimpl), without the overhead of dynamic memory allocation. For those of
you who don't know what this is, Wikipedia has a nice article you can
read. Anyway, I discovered that if you make all members in the
implementation class mutable, you can in fact use this idiom without any
"unnecessary" memory allocation. Here's a minimal example of the method:

// In the header of your class called Line

#include <string>

class Line
{
public:

Line(const std::string& name);
const std::string& GetName() const;
void SetName(const std::string& s);

private:

// Private implementation idiom:
// all member variables are hidden in this class
class LineImpl;
const LineImpl& m_pimpl; // normally a non-const pointer
};

// and in your implementation file:

#include "Line.h"

// Here we define the class with the member variables
class Line::LineImpl
{
public:

LineImpl(const std::string& s) : m_s(s) {}
// all methods need to be const here
const std::string& GetName() const { return m_s; }
void SetName(const std::string& s) const { m_s = s; }

private:

mutable std::string m_s; // the trick! all members are mutable
};

// create the pimpl instance without using new
Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}

// forward all member functions to the private implementation
const std::string& Line::GetName() const
{
return m_pimpl.GetName();
}

void Line::SetName(const std::string& s)
{
m_pimpl.SetName(s);
}

Ok experts, what do you all think? This method sacrifies
const-correctness for some extra speed. Is it worth it?

--
Daniel
  Réponse avec citation
Vieux 17/10/2007, 19h45   #2 (permalink)
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

Daniel Lidström a écrit :
> Hello!
>
> I have just discovered a way to use the private implementation idiom
> (pimpl), without the overhead of dynamic memory allocation. For those of
> you who don't know what this is, Wikipedia has a nice article you can
> read. Anyway, I discovered that if you make all members in the
> implementation class mutable, you can in fact use this idiom without any
> "unnecessary" memory allocation. Here's a minimal example of the method:
>
> // In the header of your class called Line
>
> #include <string>
>
> class Line
> {
> public:
>
> Line(const std::string& name);
> const std::string& GetName() const;
> void SetName(const std::string& s);
>
> private:
>
> // Private implementation idiom:
> // all member variables are hidden in this class
> class LineImpl;
> const LineImpl& m_pimpl; // normally a non-const pointer
> };
>
> // and in your implementation file:
>
> #include "Line.h"
>
> // Here we define the class with the member variables

[snip]
> // all methods need to be const here

[snip]
> mutable std::string m_s; // the trick! all members are mutable


Which mean you coerce the code into compilation. That's all.

> // create the pimpl instance without using new
> Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}


Your local is destroyed when going out of scope. Doesn't it ?

> [snip]
> Ok experts, what do you all think? This method sacrifies
> const-correctness for some extra speed. Is it worth it?


Not really.
And certainly not worth a dangling reference.


Michael
  Réponse avec citation
Vieux 17/10/2007, 20h07   #3 (permalink)
Daniel Lidström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

In article <4716585e$0$25087$426a74cc@news.free.fr>,
Michael DOUBEZ <michael.doubez@free.fr> wrote:

> Daniel Lidström a écrit :
> > // create the pimpl instance without using new
> > Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}

>
> Your local is destroyed when going out of scope. Doesn't it ?


No it isn't. It is actually ok to bind a temporary object to a const
reference. There will be no "dangling" reference.

--
Daniel
  Réponse avec citation
Vieux 17/10/2007, 20h09   #4 (permalink)
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

Daniel Lidström a écrit :
> In article <4716585e$0$25087$426a74cc@news.free.fr>,
> Michael DOUBEZ <michael.doubez@free.fr> wrote:
>
>> Daniel Lidström a écrit :
>>> // create the pimpl instance without using new
>>> Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}

>> Your local is destroyed when going out of scope. Doesn't it ?

>
> No it isn't. It is actually ok to bind a temporary object to a const
> reference. There will be no "dangling" reference.


It is ok to bind it but that doesn't mean the lifetime of the temporary
is extended.

Example:
const std::string& foo()
{
return std::string("bar");
}

The value returned by foo() is an dangling reference.

Michael

  Réponse avec citation
Vieux 17/10/2007, 21h36   #5 (permalink)
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

Daniel Lidström <somebody@microsoft.com> wrote:

> // In the header of your class called Line
>
> #include <string>
>
> class Line
> {
> public:
>
> Line(const std::string& name);
> const std::string& GetName() const;
> void SetName(const std::string& s);
>
> private:
>
> // Private implementation idiom:
> // all member variables are hidden in this class
> class LineImpl;
> const LineImpl& m_pimpl; // normally a non-const pointer
> };
>
> // and in your implementation file:
>
> #include "Line.h"
>
> // Here we define the class with the member variables
> class Line::LineImpl
> {
> public:
>
> LineImpl(const std::string& s) : m_s(s) {}
> // all methods need to be const here
> const std::string& GetName() const { return m_s; }
> void SetName(const std::string& s) const { m_s = s; }
>
> private:
>
> mutable std::string m_s; // the trick! all members are mutable
> };
>
> // create the pimpl instance without using new
> Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}


Where would the memory for the LineImpl object be placed? It isn't
embedded in the object, nor is it in the heap, and it can't be placed on
the stack (and still survive the call to the c_tor.)

Doesn't sound like a good idea to me.
  Réponse avec citation
Vieux 18/10/2007, 08h42   #6 (permalink)
Ulrich Eckhardt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

Daniel Lidström wrote:
> I have just discovered a way to use the private implementation idiom
> (pimpl), without the overhead of dynamic memory allocation.

[ storing a reference to a temporary ]

As you noticed, this doesn't work. However, there is a method that works.
All you have to do is to add a suitably aligned and sufficiently large
buffer into the class:

class foo {
aligned_storage<42> m_impl;
class implementation;
foo();
~foo();
void some_function();
};

class foo::implementation { ... };

foo::foo() {
// placement new
new m_impl.get<void>() implementation;
}
foo::~foo() {
// explicit dtor invokation
m_impl.get<implementation>()->~implementation;
}
void foo::some_function() {
m_impl.get<implementation>()->some_function();
}

Is it worth the hassle? Typically not, in particular since it's hard to
guarantee that you have both enough but still not too much memory.

Uli

  Réponse avec citation
Vieux 18/10/2007, 11h09   #7 (permalink)
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

On Oct 17, 10:36 pm, "Daniel T." <danie...@earthlink.net> wrote:
> Daniel Lidström <someb...@microsoft.com> wrote:
> > // In the header of your class called Line


> > #include <string>


> > class Line
> > {
> > public:
> > Line(const std::string& name);
> > const std::string& GetName() const;
> > void SetName(const std::string& s);


> > private:
> > // Private implementation idiom:
> > // all member variables are hidden in this class
> > class LineImpl;
> > const LineImpl& m_pimpl; // normally a non-const pointer
> > };


> > // and in your implementation file:


> > #include "Line.h"


> > // Here we define the class with the member variables
> > class Line::LineImpl
> > {
> > public:
> > LineImpl(const std::string& s) : m_s(s) {}
> > // all methods need to be const here
> > const std::string& GetName() const { return m_s; }
> > void SetName(const std::string& s) const { m_s = s; }
> > private:
> > mutable std::string m_s; // the trick! all members are mutable
> > };


> > // create the pimpl instance without using new
> > Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}


> Where would the memory for the LineImpl object be placed? It
> isn't embedded in the object, nor is it in the heap, and it
> can't be placed on the stack (and still survive the call to
> the c_tor.)


>From the standard (§12.2/5): "A temporary bound to a reference

member in a constructor's ctor-initializer persists until the
constructor exits." In colloquial terms: the temporary is
created on the stack, and destructed before returning from the
constructor.

> Doesn't sound like a good idea to me.


It isn't, unless you like undefined behavior and hard to find
bugs.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


  Réponse avec citation
Vieux 18/10/2007, 14h59   #8 (permalink)
Yannick Tremblay
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Pimpl idiom without dynamic memory allocation

In article <47165e09$0$26728$426a74cc@news.free.fr>,
Michael DOUBEZ <michael.doubez@free.fr> wrote:
>Daniel Lidström a écrit :
>> In article <4716585e$0$25087$426a74cc@news.free.fr>,
>> Michael DOUBEZ <michael.doubez@free.fr> wrote:
>>
>>> Daniel Lidström a écrit :
>>>> // create the pimpl instance without using new
>>>> Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}
>>> Your local is destroyed when going out of scope. Doesn't it ?

>>
>> No it isn't. It is actually ok to bind a temporary object to a const
>> reference. There will be no "dangling" reference.

>
>It is ok to bind it but that doesn't mean the lifetime of the temporary
>is extended.
>
>Example:
>const std::string& foo()
>{
> return std::string("bar");
>}
>
>The value returned by foo() is an dangling reference.
>


Euh, it's not what he is doing, it's more like:

std::string foo()
{
return std::string("bar");
}

int main()
{
std::string const & val = foo();
....


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h49.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15357 seconds with 16 queries