static template variable ???

I

Ips

hello!

I'm wondering if the following compiler behaviour is correct and standard
compliant.
I've used three compilers to compile this code and two of them(gcc,
MSVC++6.0) are not complaining about anything (even no warnings issued).
The problem is the third complier (Code Composer Studio 2.0 by Texas
Instuments) gives me this :
"error: specializing member "Container<T>::m_map [with T=float]" without
"template<>" syntax is nonstandard"

here's the code :
// map.h
template<typename K,typename V> class Map
{
// some stuff here
};
// function definitions

//container.h

#include "map.h"
template<typename T> class Container
{
static Map<int,T*> m_map;
public:

static int createObject(int key);
static T* getReference(int key);
static void destroyObject(int key);
};
// function definitions

//main.cpp

#include "container.h"

Map<int,float*> Container<float>::m_map;

int main()
{
// some stuff here, something like:
Container<float>::createObject(0);
float* pFloat = Container<float>::getReference(0);
if(pFloat)
*pFloat = 453.43f;

Container<float>::destroyObject(0);

}

The point is I have to use the Code Composer Studio and I have to use the
Container class as designed above.
Can you tell me if the code is 100% correct or have I made a mistake
somewhere???
Is there any workaroud for this?

regards,
Ips
 
I

Ivan Vecerina

Ips said:
hello!

I'm wondering if the following compiler behaviour is correct and standard
compliant.
I've used three compilers to compile this code and two of them(gcc,
MSVC++6.0) are not complaining about anything (even no warnings issued).
The problem is the third complier (Code Composer Studio 2.0 by Texas
Instuments) gives me this :
"error: specializing member "Container<T>::m_map [with T=float]" without
"template<>" syntax is nonstandard"
Which is a correct error message. In strict mode, I would hope that gcc also
complains.
template<typename T> class Container
{
static Map<int,T*> m_map; ....
[...]


Map<int,float*> Container<float>::m_map;
Incorrect. You probably want to use:
// instantiate the static data member for any instance of the Container<T>
template:
template<class T>
Map<int,T*> Container<T>::m_map;
// The compiler will now generate the variable whenever needed.

Or eventually the template specialization for Container<float> only:
template<> // specialized implementation for Container<float> only
Map<int,float*> Container<float>::m_map;

This should work in all compliant C++ compilers.

hth,
Ivan
 
I

Ips

template<typename T> class Container
{
static Map<int,T*> m_map; ...
[...]


Map<int,float*> Container<float>::m_map;
Incorrect. You probably want to use:
// instantiate the static data member for any instance of the
Container said:
template:
template<class T>
Map<int,T*> Container<T>::m_map;
// The compiler will now generate the variable whenever needed.

you're right. I did so but I had to move this line to another file. (now it
works fine)
Or eventually the template specialization for Container<float> only:
template<> // specialized implementation for Container<float> only
Map<int,float*> Container<float>::m_map;

I put this float-specialized container as a sample. As I didn't manage to
compile the general template definition I wanted to compile only the float
one to see if it works. Obviously it wasn't what I really wanted to achive
:)

thanks for response
regards,
Ips
 

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

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top