471,049 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

static template variable ???

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
Jul 22 '05 #1
2 1597
"Ips" <ko*****@poczta.onet.pl> wrote in message
news:cd**********@newshost.mot.com...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
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<T> 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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Graham Dumpleton | last post: by
7 posts views Thread by Drew McCormack | last post: by
16 posts views Thread by Eric | last post: by
7 posts views Thread by ank | last post: by
4 posts views Thread by santosh | last post: by
3 posts views Thread by Diebels | last post: by
17 posts views Thread by Juha Nieminen | last post: by

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.