473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Template static member initialization

Hi!

My template class has a const static data member, which is used by the
constructor. It seems that the program does not produce the correct
result. Either it is the fault of the compiler (gcc 4.1) or I need to
learn more about initialization of static members... The code below
captures the problem and is as simple as I could make it:

#include <iostream>
#include <limits>

template<typename Tstruct A {
const static unsigned NONE;
unsigned x;
A() { x = NONE; }
};

template<typename Tconst unsigned A<T>::NONE =
numeric_limits<unsigned>::max();
A<doublea;

int main(void) {
cout << a.x << "\n";
cout << A<double>::NONE << "\n";
return 0;
}

The output of the program is:
0
4294967295

Mustn't NONE be initialized before a is constructed?

Cheers,
/ALiX

Jul 26 '07 #1
11 3326
ALiX wrote:
My template class has a const static data member, which is used by the
constructor. It seems that the program does not produce the correct
result. Either it is the fault of the compiler (gcc 4.1) or I need to
learn more about initialization of static members... The code below
captures the problem and is as simple as I could make it:

#include <iostream>
#include <limits>

template<typename Tstruct A {
const static unsigned NONE;
unsigned x;
A() { x = NONE; }
};

template<typename Tconst unsigned A<T>::NONE =
numeric_limits<unsigned>::max();
std::numeric_limits
A<doublea;

int main(void) {
Drop the 'void' -- bad style
cout << a.x << "\n";
cout << A<double>::NONE << "\n";
return 0;
}

The output of the program is:
0
4294967295

Mustn't NONE be initialized before a is constructed?
It's not *instantiated* unless it's "used". Perhaps referring to
it in the c-tor does not get it instantiated (and therefore does
not get it initialised), but I would consider it a compiler bug.

Try initialising it right in the class:

template<typename Tstruct A {
static unsigned const NONE
= std::numeric_limits<unsigned>::max();
unsigned x;
A() : x(NONE) {}
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #2
On Jul 26, 4:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Try initialising it right in the class:

template<typename Tstruct A {
static unsigned const NONE
= std::numeric_limits<unsigned>::max();
unsigned x;
A() : x(NONE) {}
};
Then I get an error:
error: 'std::numeric_limits<unsigned int>::max()' cannot appear in a
constant-expression
error: a function call cannot appear in a constant-expression

Making NONE non-const does not help either as non-const static members
cannot be initialized in the class declaration.

/ALiX
Jul 26 '07 #3
This seems to work. However, it gives me another headache since in the
real application I'm writing, NONE is actually public (I should have
made it public in the example code... sorry!). I use it like
std::string::npos to return 'end-values'. I'd like a solution that
allows me to keep NONE as a public static member of the class A.

Cheers,
ALiX

Jul 26 '07 #4
ALiX wrote:
This seems to work. However, it gives me another headache since in the
real application I'm writing, NONE is actually public (I should have
made it public in the example code... sorry!). I use it like
std::string::npos to return 'end-values'. I'd like a solution that
allows me to keep NONE as a public static member of the class A.
Don't initialise it with 'max()', initialise it with -1.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #5
On Jul 26, 5:36 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Don't initialise it with 'max()', initialise it with -1.
Do I still need to define NONE outside the class declaration? Are we
always guaranteed that ((unsigned)-1) ==
numeric_limts<unsigned>::max()?

/ALiX
Jul 26 '07 #6
ALiX wrote:
On Jul 26, 5:36 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Don't initialise it with 'max()', initialise it with -1.

Do I still need to define NONE outside the class declaration?
Not unless you take its address anywhere.
Are we
always guaranteed that ((unsigned)-1) ==
numeric_limts<unsigned>::max()?
Yep.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #7
Thanks! Your help is much appreciated.

Cheers,
/ALiX

Jul 26 '07 #8
On Jul 26, 5:52 pm, ALiX <alix.tof...@gmail.comwrote:
On Jul 26, 5:36 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Don't initialise it with 'max()', initialise it with -1.
Do I still need to define NONE outside the class declaration? Are we
always guaranteed that ((unsigned)-1) ==
numeric_limts<unsigned>::max()?
Yes and yes. You can also use UINT_MAX, which is guaranteed to
be an integral constant expression.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 27 '07 #9
ALiX a écrit :
On Jul 26, 5:36 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Don't initialise it with 'max()', initialise it with -1.

Do I still need to define NONE outside the class declaration?
If it clarifies the purpose of A, you may alias it in your class:

template<typename T>
struct A
{
static const unsigned int NONE=A_traits<T>::NONE;
//...
};
Michael
Jul 27 '07 #10
On 2007-07-27 04:11:03 -0400, James Kanze <ja*********@gmail.comsaid:
That's only legal if the initializer is a constant expression.
A constant expression cannot (at present, anyway) contain a
function call, even if the function is inline, and only returns
a constant value.
To elaborate a bit on the "at present, anyway": at last week's
standards meeting two proposals concerning constant expressions were
voted into the next standard. One permits calling appropriately
decorated functions as part of a constant expression, and the other
adds those decorations to various functions in the standard library,
including the members of the numeric_limits explicit instantiations. So
std::numeric_limits<unsigned>::max() will be acceptable as a static
initializer in C++0x.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jul 27 '07 #11
X-No-Archive: yes
James Kanze wrote:
On Jul 26, 8:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Victor Bazarov wrote:
>>ALiX wrote:

[...]
>Curiously enough, it might just be QoI issue. I built your code with
VC++ 2005 SP1. In Debug mode it gave me your output, and in Release
mode it gave the correct output... Go figure.

What part of "undefined" don't you understand? [..]
Please refrain from using this tone with me. Thank you.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 27 '07 #12

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

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
7
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
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...
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
3
by: Serge Skorokhodov (216716244) | last post by:
Hi, I just seeking advice. Some background information first because I've run into issues that seems pretty obscure to me:( Quick search through KB yields next to nothing. Simplified samples...
8
by: Markus Henschel | last post by:
Hello, this is a test case of something I just can't explain myself: ...
5
by: Gernot Frisch | last post by:
// - in my xy.cpp file -- template <const int Ttex, const int Tcol, const int Tlight, int TzBuffer> struct MyFragmentShader { static const int varying_count = Ttex*2 + Tcol*3 + Tlight; }; ...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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
Oralloy
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.