473,486 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to declare static members of a template class

I tried this code:

#include <iostream>
#include <string>

template<typename T>
struct enum_properties
{
static const long max;
static const std::string name;
};

enum MyEnum { a,b,c };

int main()
{
std::cout << "My enum is called " << enum_properties<MyEnum>::name
<< " and its max value is " << enum_properties<MyEnum>::max
<< std::endl;
}

const long enum_properties<MyEnum>::max (2);
const std::string enum_properties<MyEnum>::name ("MyEnum");

but it gets a parse error on the last 2 lines.

I found out that to fix the problem, I have to add in the
following lines, AFTER the declaration of MyEnum
but BEFORE the first usage of enum_properties<>::name :

template<>
struct enum_properties<MyEnum>
{
static const long max;
static const std::string name;
};

This seems like senseless duplication to me, I have to
repeat all this code for every different type that I
want to be a template parameter. Also, it can be difficult
in a large project to ensure that this code block occurs
after the enum declaration but before the first instantiation
of it, eg. if there is code in header files that refers to it.

Is there any better solution?

Jul 23 '05 #1
1 2334
Old Wolf wrote:
I tried this code:

#include <iostream>
#include <string>

template<typename T>
struct enum_properties
{
static const long max;
static const std::string name;
};

enum MyEnum { a,b,c };

int main()
{
std::cout << "My enum is called " << enum_properties<MyEnum>::name
<< " and its max value is " << enum_properties<MyEnum>::max
<< std::endl;
}

const long enum_properties<MyEnum>::max (2);
const std::string enum_properties<MyEnum>::name ("MyEnum");

but it gets a parse error on the last 2 lines.
The 'enum_properties<MyEnum>' class hasn't been defined, that's probably
the reason why definitions of those static members fail.

I found out that to fix the problem, I have to add in the
following lines, AFTER the declaration of MyEnum
but BEFORE the first usage of enum_properties<>::name :

template<>
struct enum_properties<MyEnum>
{
static const long max;
static const std::string name;
};
That's called "a specialization".
This seems like senseless duplication to me, I have to
repeat all this code for every different type that I
want to be a template parameter. Also, it can be difficult
in a large project to ensure that this code block occurs
after the enum declaration but before the first instantiation
of it, eg. if there is code in header files that refers to it.

Is there any better solution?


Static data members need to be defined. (Have you not heard of that
before?) If your 'max' is always the same, then after your class,
before the 'MyEnum' declaration, write

template<class T> const long enum_properties<T>::max(2); // ...

If they are not the same, you may explicitly _specify_ only those
members that you need. The syntax is similar to the specialisation:

template<> const long enum_properties<MyEnum>::max(2);

and those will be the definitions for the static members of your
_implicitly_ specialised enum_properties<MyEnum> class.

V
Jul 23 '05 #2

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

Similar topics

3
1613
by: Alibek | last post by:
Hello ! I have here template class with static members. and I put definition of the static member in the same ..h file where the class was declared. however, we are getting very stange...
7
2457
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
18
5728
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
13
7665
by: Adam H. Peterson | last post by:
I just made an observation and I wondered if it's generally known (or if I'm missing something). My observation is that static protected members are essentially useless, only a hint to the user. ...
9
1491
by: Rennie deGraaf | last post by:
I'm writing a class similar to this: #include <iostream> using namespace std; template<class t> class Foo { private: class Bar {
5
3580
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
8
2755
by: John | last post by:
Hello, is there any compiler option for g++ for initializing static members of the class. Due to some unknown reason, static member in one of our c++ application is not getting initialized...
10
1438
by: =?Utf-8?B?Y2FybG0=?= | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
17
8349
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
6967
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...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
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...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
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...
0
4564
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...
0
1381
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 ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
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...

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.