473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Pimpl idiom and member function templates

Hello!

I was playing around pimpl idiom and discovered that there is a problem
with it if a class member template function exists which has to access
private data, since only the forward declaration of pimpl class is
provided.
Example:
class Foo
{
private:
struct foo_impl;
foo_impl *pimpl;

public:
// ...
//

template <typename T>
T bar()
{
// bar() has to access private data which is located
// in pimpl.
// for example,

int id = pimpl->baz;

// ...
}
};

To allow the template be instantiated for any type is has to be
left in the interface of Foo class. But then it cannot access
pimpl->baz because pimpl has only been forward declared.

I have three solutions but I do not like any of them.

The first solution is to include or write the definition of
foo_impl class in the same file where Foo's interface is,
so Foo::foo_impl was defined.

I don't like because it would not reduce compile times and probably
will make client code to be recompiled since header file would have
changed since last compile and most tools for automatic compilation
look at modification times.

The second solution is to create a global function which takes
a reference to Foo and make this function a friend to Foo.

Example,
class Foo {
// ...
template <typename T>
friend int bar(Foo &foo);
// ...
};

and somewhere
template <typename T> int bar(Foo &foo) { return foo->impl->baz; }

As bar() is friend to Foo, it can access private pimpl.

I don't like this solution because i break interface of Foo, instead
of a convenient member function a global has to be added and an object
has to be passed to this function.

The third solution is the simplest, bring the particular private member
which has to be accessed in template function back to private section
of Foo.
I don't like it cause I have to expose implementation details which is
what pimpl helps me not to do.
Can anyone think of more solutions to this problem? Maybe there is
a direct solution which allows somehow to keep the original
design and access pimpl members?
Thanks!
P.Krumins

Aug 31 '05 #1
2 6078

Peteris Krumins wrote:
Hello!

I was playing around pimpl idiom and discovered that there is a problem
with it if a class member template function exists which has to access
private data, since only the forward declaration of pimpl class is
provided.

[snip]


if the number of possible template arguments is small, you can do
explicit template instantiation:

// .h file:

class Foo {
public:
template<class T>
void bar(T x);
};

// .cpp file:
template void Foo::bar(int);
template void Foo::bar(long);
template void Foo::bar(float);

Otherwise, you're trying to mix two things that are incompatiable:
separate compilation and templating. You COULD also get one of those
rare compilers which support the export keyword... (comeau is the only
one I know of) Of course, so much for portability in that case.

Aug 31 '05 #2
>
if the number of possible template arguments is small, you can do
explicit template instantiation:

[snip]

Otherwise, you're trying to mix two things that are incompatiable:
separate compilation and templating. You COULD also get one of those
rare compilers which support the export keyword... (comeau is the only
one I know of) Of course, so much for portability in that case.


Thanks!

Unfortunately that member template function works like a factory and
I can't explicitly instantiate because I don't know all types which
will
be constructed with it.

I chose to bring one of the pimpl members back to the class to solve
the problem till more compilers start supporting export keyword!
P.Krumins

Aug 31 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
6
by: Asfand Yar Qazi | last post by:
Hi, Now that GCC 3.4 has precompiled headers, I'm thinking I can stop using pimpls to speed up development time, as it may make life easier (declaring pimpls takes a long time...) What are...
2
by: Debajit Adhikary | last post by:
I'm still pretty new to design patterns... I was wondering, is there any difference between the Bridge Pattern and Herb Sutter's Pimpl Idiom? Both delegate responsibility to an implementation...
9
by: Edward Diener | last post by:
Because 'friend' is not recognized in MC++, using the pImpl idiom in MC++ classes seems nearly impossible. Normally a pImpl class is a 'friend' to the class for which it supplies the private...
10
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
4
by: Noah Roberts | last post by:
Some little tidbit I just ran into that might help some, especially novice programmers. If you are using the pimpl idiom, as you probably should be most of the time, then it is very...
14
by: Daniel Lidström | last post by:
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...
2
by: Graham Reitz | last post by:
What are good strategies for selecting, either at run-time or compile time, various pimpl'ed implementations? While retaining the ability to switch implementations without recompiling. Boost...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.