Hey all,
So I have a template class whose only template parameter is a type that the
class is to manage. It's actually an implementation of the common Singleton
design pattern. The class looks like this:
template< class Type >
class Singleton
{
public:
static Type& instance();
private:
static Type* _instance;
};
The instance() method looks like this:
Type& Singleton::inst ance()
{
if( _instance == NULL )
{
_instance = new Type();
}
return *_instance;
}
So, if I wanted to make a singleton class, it would just have to inherit from
this template class like so:
class Tableau : public Singleton< Tableau >
{
private:
// Make the constructor private so other parts of the
// code can't create a new instance
Tableau();
// Make the Singleton class a friend so that it can
// create the static instance of this guy.
friend Singleton< Tableau >;
};
This all works fine with MS Visual C++ .Net. This also used to work with gcc.
However, I tried to compile this with gcc 3.4.4 for cygwin, and no go. I get
the following error.
tableau.h:35: error: a class-key must be used when declaring a friend
A quick google for that error message and I see that newer versions of gcc do
not allow these so-called "ill-formed type" to be friends. However, I can't
find a fix or a work-around for this.
So, can anyone suggest a way to make make my singleton template more
platform-independent?
Thanks.
Dave 2 5522
Dave Rudolf wrote:
Hey all,
So I have a template class whose only template parameter is a type that
the class is to manage. It's actually an implementation of the common
Singleton design pattern. The class looks like this:
template< class Type >
class Singleton
{
public:
static Type& instance();
private:
static Type* _instance;
};
The instance() method looks like this:
Type& Singleton::inst ance()
{
if( _instance == NULL )
{
_instance = new Type();
}
return *_instance;
}
So, if I wanted to make a singleton class, it would just have to inherit
from this template class like so:
class Tableau : public Singleton< Tableau >
{
private:
// Make the constructor private so other parts of the
// code can't create a new instance
Tableau();
// Make the Singleton class a friend so that it can
// create the static instance of this guy.
friend Singleton< Tableau >;
};
This all works fine with MS Visual C++ .Net. This also used to work with
gcc. However, I tried to compile this with gcc 3.4.4 for cygwin, and no
go. I get the following error.
tableau.h:35: error: a class-key must be used when declaring a friend
A quick google for that error message and I see that newer versions of
gcc do not allow these so-called "ill-formed type" to be friends.
However, I can't find a fix or a work-around for this.
So, can anyone suggest a way to make make my singleton template more
platform-independent?
Thanks.
Dave
template and friends are not my strength, and it seems so obvious that
I'm probably wrong, but have you tried
friend class Singleton< Tableau >;
?
john
On Feb 22, 5:36 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
Dave Rudolf wrote:
Hey all,
So I have a template class whose only template parameter is a type that
the class is to manage. It's actually an implementation of the common
Singleton design pattern. The class looks like this:
template< class Type >
class Singleton
{
public:
static Type& instance();
private:
static Type* _instance;
};
The instance() method looks like this:
Type& Singleton::inst ance()
{
if( _instance == NULL )
{
_instance = new Type();
}
return *_instance;
}
So, if I wanted to make a singleton class, it would just have to inherit
from this template class like so:
class Tableau : public Singleton< Tableau >
{
private:
// Make the constructor private so other parts of the
// code can't create a new instance
Tableau();
// Make the Singleton class a friend so that it can
// create the static instance of this guy.
friend Singleton< Tableau >;
};
This all works fine with MS Visual C++ .Net. This also used to work with
gcc. However, I tried to compile this with gcc 3.4.4 for cygwin, and no
go. I get the following error.
tableau.h:35: error: a class-key must be used when declaring a friend
A quick google for that error message and I see that newer versions of
gcc do not allow these so-called "ill-formed type" to be friends.
However, I can't find a fix or a work-around for this.
So, can anyone suggest a way to make make my singleton template more
platform-independent?
Thanks.
Dave
template and friends are not my strength, and it seems so obvious that
I'm probably wrong, but have you tried
friend class Singleton< Tableau >;
?
john- Hide quoted text -
- Show quoted text -
Yes, so obviously wrong that you are right :). That actually did it.
Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
by: Bob Hairgrove |
last post by:
Hello,
Consider the member function length() in the following declaration:
enum range_direction { NONE, FORWARD, REVERSE };
template<typename E, typename D>
class Range
{
typedef E element_type;
|
by: Matt Taylor |
last post by:
I am trying to use templates to create an optimal structure for a fixed-size
buffer that holds a homogenous array of elements. For brevity, this problem
is somewhat simplified from my implementation:
template<typename T1, size_t len>
struct buffer_t
{
// Array of elements
T1 ary;
|
by: wkaras |
last post by:
I would like to propose the following changes to the C++ Standard,
the goal of which are to provide an improved ability to specify
the constraints on type parameters to templates. Let me say from
the start that my knowledge of compiler implementation is very
limited. Therefore, my suggestions may have to be rejected
because they are...
|
by: Axter |
last post by:
I'm fine tuning a scope_handle class that takes a policy class as the
second template.
http://code.axter.com/scope_handle.h
Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is
setup is that for the ptr_policy class the first template type is
different from the...
| |
by: Sven Groot |
last post by:
This was posted by someone in comp.lang.c++, and later in
microsoft.public.vstudio.general, but since I know Carl is in this group,
and he's the one that should read this, I've reposted it here. I've also
minimalised the code that causes the bug.
The bug is that the following syntax causes an Internal Compiler Error. The
code is not...
|
by: john |
last post by:
Hi to All,
Suppose I would like to implement a generic class with three type
parameters:
class UniversalClass<TA, TB, TC>()
where TA: A, new()
where TB: B, new()
where TC: C, new()
|
by: Mike |
last post by:
Hi,
Just a simple question: why the compiler doesn't report error when
accessing a private member function inside a function having template
type ?
For example:
#include<iostream>
using namespace std;
|
by: Ralf Goertz |
last post by:
Hi,
I want to do able to declare an object bar of a templated class foo
where the actual template type of foo is only know at runtime:
-----
#include <string>
template <class Tclass foo {
|
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...
|
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...
| |
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. ...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |