Templates gcc 3.4

S

Simon White

I've just upgraded to gcc 3.4 resulting in problems in some template
code I have. The code shown below is a simplified version of what we
are using to wrap the creation of singletons.

#include <new>

template <class T>
class foo
{
public:
static T& Instance();

private:
static T* _me;
static char _buildHere[];
};

template <class T>
inline T& foo<T>::Instance()
{
if (0 == _me)
_me = new(_buildHere) T;
return *_me;
}

class bar
{
};

bar* foo< bar >::_me = 0;
char foo< bar >::_buildHere[sizeof(bar)];
bar &t = foo<bar>::Instance ();

int main ()
{
return 0;
}

It complains about:
test.cpp:26: error: too few template-parameter-lists
test.cpp:26: error: expected `,' or `;' before '=' token
test.cpp:27: error: too few template-parameter-lists

Doing a search for information appears that we need to add template<> to
get the error to disappear, however doing so now causes a link problem
claiming the _buildHere symbol is undefined (_me seems to be ok). I
also tried this modified code in gcc 3.3 resulting in the same problem.

Is this a problem with the code and if so any ideas on how to fix it?
 
V

Victor Bazarov

Simon said:
I've just upgraded to gcc 3.4 resulting in problems in some template
code I have. The code shown below is a simplified version of what we
are using to wrap the creation of singletons.

#include <new>

template <class T>
class foo
{
public:
static T& Instance();

private:
static T* _me;
static char _buildHere[];
};

template <class T>
inline T& foo<T>::Instance()
{
if (0 == _me)
_me = new(_buildHere) T;
return *_me;
}

class bar
{
};

bar* foo< bar >::_me = 0;

Has to have 'template said:
char foo< bar >::_buildHere[sizeof(bar)];

Changing to

template<> char foo<bar>::_buildHere[sizeof(bar)] = { 0 };

You need an initialiser to make it a definition.
bar &t = foo<bar>::Instance ();

int main ()
{
return 0;
}

It complains about:
test.cpp:26: error: too few template-parameter-lists
test.cpp:26: error: expected `,' or `;' before '=' token
test.cpp:27: error: too few template-parameter-lists

Doing a search for information appears that we need to add template<> to
get the error to disappear, however doing so now causes a link problem
claiming the _buildHere symbol is undefined (_me seems to be ok). I
also tried this modified code in gcc 3.3 resulting in the same problem.

Is this a problem with the code and if so any ideas on how to fix it?

It's not a problem with the code, it's the inherent problem with fully
specialised template members.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top