473,786 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bound template parameter as argument to friend member functiondeclara tion

Ok, I tried to make that subject as descriptive as possible.

What I'm trying to do:

I'm attempting to use policies to create a generic memento (design
pattern) template. My Memento template so far is pretty simple:

using an idea of Andrei Alexandrescu (at least that's where I first read
it) to create a lightweight way of overloading member functions since
member function specialization is not possible.

template <typename T>
struct Type2Type {
typedef T OriginalType;
};

template <typename Originator,
typename CmdObj,
typename StorageType>
class Memento
{
public:
friend Memento Originator::Cre ateMemento(Type 2Type<CmdObj>);
//other junk
};

My point in doing this is to force a compile-time check that Originator
has a CreateMemento function that takes an argument of type Type2Type<T>.
The main reason I decided to make just the member function CreateMemento a
friend is because a statement like this:

friend class Originator; //or this...
friend Originator;

is illegal in c++. If it's an entire class we're making a friend, the
friend syntax specifies that you have to include the word 'class.'
However, in this case, adorning the template parameter Originator, with
the word 'class,' is illegal (I forget the exact reason, but regardless,
not the point).

The problem:

I instantiate this template like this:

typedef Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >
DataMemento;

When I compile this with Sun Workshop 7, it results in an error, saying
that in the friend declaration above:

friend Memento Originator::Cre ateMemento(Type 2Type<CmdObj>);

CmdObj is not defined. This really doesn't seem like it should be a
problem. Any thoughts here?!

Could it be a problem with forward declarations vs. the need for the whole
interface to be visible?

This has been bugging me big time, so any help is appreciated.

Justin

-----------------------------------------

Programmer/Analyst
NASA/Goddard Space Flight Center Code 660
email: mi*****@milkywa y.gsfc.nasa.gov
office: 301-286-9261

Jul 22 '05 #1
4 2446
On Mon, 1 Nov 2004 17:13:55 -0500, Justin Miller
<mi*****@milkyw ay.gsfc.nasa.go v> wrote:
When I compile this with Sun Workshop 7, it results in an error, saying
that in the friend declaration above:

friend Memento Originator::Cre ateMemento(Type 2Type<CmdObj>);

CmdObj is not defined. This really doesn't seem like it should be a
problem. Any thoughts here?!

Could it be a problem with forward declarations vs. the need for the whole
interface to be visible?


This compiles fine on Comeau and GCC:

#include <vector>

template <typename T>
struct Type2Type {
typedef T OriginalType;
};

template <typename Originator,
typename CmdObj,
typename StorageType>
class Memento
{
public:
friend Memento Originator::Cre ateMemento(Type 2Type<CmdObj>);
//other junk
};

class DataCmdObj;
class SpectralData;
struct DataContainer
{
Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >
CreateMemento(T ype2Type<DataCm dObj>){ return
Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >(); }
};

int main()
{
Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >
DataMemento;
}

I believe that Sun's compiler isn't quite as good compliance-wise as
the latest efforts of its rivals...

Tom
Jul 22 '05 #2
Yeah, we've tested on gcc as well, and agreed, it does compile fine.
Unfortunately, we have to support SUN as well - even though it seems to
be causing more and more problems for us. I wish we could just scrap that
compiler altogether, or upgrade to the latest version. We're still
supporting workshop 6! We've had a lot of standard-compliance issues.

Any thoughts about a workaround for this?

Justin

-----------------------------------------

Programmer/Analyst
NASA/Goddard Space Flight Center Code 660
email: mi*****@milkywa y.gsfc.nasa.gov
office: 301-286-9261

On Tue, 2 Nov 2004, Tom Widmer wrote:

This compiles fine on Comeau and GCC:

#include <vector>

template <typename T>
struct Type2Type {
typedef T OriginalType;
};

template <typename Originator,
typename CmdObj,
typename StorageType>
class Memento
{
public:
friend Memento Originator::Cre ateMemento(Type 2Type<CmdObj>);
//other junk
};

class DataCmdObj;
class SpectralData;
struct DataContainer
{
Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >
CreateMemento(T ype2Type<DataCm dObj>){ return
Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >(); }
};

int main()
{
Memento<DataCon tainer, DataCmdObj, std::vector<Spe ctralData*> >
DataMemento;
}

I believe that Sun's compiler isn't quite as good compliance-wise as
the latest efforts of its rivals...

Tom


Jul 22 '05 #3
On Tue, 2 Nov 2004 09:00:47 -0500, Justin Miller
<mi*****@milkyw ay.gsfc.nasa.go v> wrote:
Yeah, we've tested on gcc as well, and agreed, it does compile fine.
Unfortunatel y, we have to support SUN as well - even though it seems to
be causing more and more problems for us. I wish we could just scrap that
compiler altogether, or upgrade to the latest version. We're still
supporting workshop 6! We've had a lot of standard-compliance issues.

Any thoughts about a workaround for this?


I'm afraid I don't have sun CC to experiment with, so not really. Does
it compile ok if DataCmdObj is a completely defined type? How about if
you change your check to something like:

template <typename Originator,
typename CmdObj,
typename StorageType>
class Memento
{
private:
static Type2Type<CmdOb j> makeType2TypeCm dObj();
static Originator makeOriginator( );
static char takesMemento(Me mento const&);

static int const check =
sizeof(

takesMemento(ma keOriginator(). CreateMemento(m akeType2TypeCmd Obj()))
) == 1;

public:
//etc.
};

Tom
Jul 22 '05 #4
Mmmm. This is an almost perfect solution for me. It certainly enforces
the policy, however, I still need for ONLY the CreateMemento function
of the Originator to be able to access the private members of the
Memento object. Any way for that to happen?

The friend declaration would have been perfect (but Sun is stupid).
One could do it Java style by making a new (possibly nested) memento
class for each command that needs one. I was hoping to avoid this by
using the template (maybe I was being overly optimistic, thinking that
a memento would only need to store one data type... could be extended
to two types w/ little difficulty).

Excellent suggestion though. I may end up using parts of it.

Justin
I'm afraid I don't have sun CC to experiment with, so not really. Does
it compile ok if DataCmdObj is a completely defined type? How about if
you change your check to something like:

template <typename Originator,
typename CmdObj,
typename StorageType>
class Memento
{
private:
static Type2Type<CmdOb j> makeType2TypeCm dObj();
static Originator makeOriginator( );
static char takesMemento(Me mento const&);

static int const check =
sizeof(

takesMemento(ma keOriginator(). CreateMemento(m akeType2TypeCmd Obj()))
) == 1;

public:
//etc.
};

Tom

Jul 22 '05 #5

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

Similar topics

4
5557
by: Bonzo | last post by:
I'm trying to write a class for a smart pointer. I'm having a problem though when trying to implement operator== with both sides being a Smart. (Or any function that I try to bring U into.) The pointer contained might not be the exact same type but still get tested for equality. Here's what I have: template <typename T> class Smart; template<typename T, typename U>
1
3344
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
2
2086
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with something like below: class PMinimum { public: template <typename T> bool operator()(const T & a, const T & b)
5
1644
by: Gianni Mariani | last post by:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1 dies complaining about somthing <unknown>. Is this valid ? More to the point, is there any way of doing this that is supported across all 3 compilers ? I want a template to take a parameter and make it it's friend. <code>
5
2640
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
2
2508
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
16
3958
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
4
1538
by: Christof Warlich | last post by:
Hi, I need to make a templated class A a friend of class B. My problem: I think I know how to do this for both 1) _one_ specific template instantiation of class A, e.g.: template<typename Type, unsigned int xclass A { public: void useSomeFunctionalityOfB(void);
8
284
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.