473,396 Members | 2,099 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,396 software developers and data experts.

Defining static members in explicit specializations

I'm writing a class similar to this:

#include <iostream>
using namespace std;

template<class t> class Foo
{
private:
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}
};
static Bar x;
t y;
};

template<> Foo<int>::Bar Foo<int>::x;

int main()
{
return 0;
}

When executed, it should print "hello, world" to the console, which it
does when compiled with Sun's CC compiler. However, it does nothing
when compiled with GCC (versions 4.0.2 and older). GCC-compiled
versions produce the expected output when Foo is made into a normal,
rather than template, class, or when Foo::x is made into a pointer and
initialized with a dynamically-allocated object. Is this a bug in GCC,
or one of those poorly-specified edge cases in C++?

Rennie
Dec 30 '05 #1
9 1487
If you change the line:

template<> Foo<int>::Bar Foo<int>::x:

to

Foo<int>::Bar Foo<int>::x;

it works fine. The syntax you used is for partial template
specialization, which is not what you want here. All you want is to
refer to (and initialize) the (static) member named "x", of type
Foo<int>::Bar, of class Foo<int>.

Luke

Dec 30 '05 #2
AD
I made slight modifications to your program and it worked.
a) I changed the initialisation of static member of template class Foo
as following:
"template<class t> Foo<t>::Bar Foo<t>::x;"
This initializes x for all typenames and not only int.
b) I added a function to class Bar and then used x in Foo to call this
function.
This caused x to be instantiated and Bar constructor was called. This
is explained by concept that template members are initialised at the
first point of use in program and not necessarily at the
definition/declaration. So mere declaration/definition of static member
of template class wont instantiate it.

Following is the changed program and it worked with g++ compiler.

#include <iostream>
using namespace std;

template<class t> class Foo
{
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}

void Call()
{
cout<<"Bar::Call"<<endl;
}
};
public:
static Bar x;
t y;

};

template<class t> Foo<t>::Bar Foo<t>::x;

int main()
{
Foo<int> f;
Foo<int>::x.Call();

return 0;
}
/**************************/

Dec 30 '05 #3
Luke Meyers wrote:
If you change the line:

template<> Foo<int>::Bar Foo<int>::x:

to

Foo<int>::Bar Foo<int>::x;

it works fine. The syntax you used is for partial template
specialization, which is not what you want here. All you want is to
refer to (and initialize) the (static) member named "x", of type
Foo<int>::Bar, of class Foo<int>.

Luke


If I do that, gcc barfs with this message:

test.cpp:19: error: too few template-parameter-lists

Rennie
Dec 30 '05 #4
AD wrote:
I made slight modifications to your program and it worked.
a) I changed the initialisation of static member of template class Foo
as following:
"template<class t> Foo<t>::Bar Foo<t>::x;"
This initializes x for all typenames and not only int.
b) I added a function to class Bar and then used x in Foo to call this
function.
This caused x to be instantiated and Bar constructor was called. This
is explained by concept that template members are initialised at the
first point of use in program and not necessarily at the
definition/declaration. So mere declaration/definition of static member
of template class wont instantiate it.

Following is the changed program and it worked with g++ compiler.

#include <iostream>
using namespace std;

template<class t> class Foo
{
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}

void Call()
{
cout<<"Bar::Call"<<endl;
}
};
public:
static Bar x;
t y;

};

template<class t> Foo<t>::Bar Foo<t>::x;

int main()
{
Foo<int> f;
Foo<int>::x.Call();

return 0;
}
/**************************/


g++ 4.0.2 throws the following error on that code (although g++ 2.95.3
accepts it):

test.cpp:25: error: expected constructor, destructor, or type conversion
before ‘Foo’

Rennie
Dec 30 '05 #5

Rennie deGraaf wrote:
AD wrote:
I made slight modifications to your program and it worked.
a) I changed the initialisation of static member of template class Foo
as following:
"template<class t> Foo<t>::Bar Foo<t>::x;"
This initializes x for all typenames and not only int.
b) I added a function to class Bar and then used x in Foo to call this
function.
This caused x to be instantiated and Bar constructor was called. This
is explained by concept that template members are initialised at the
first point of use in program and not necessarily at the
definition/declaration. So mere declaration/definition of static member
of template class wont instantiate it.

Following is the changed program and it worked with g++ compiler.

#include <iostream>
using namespace std;

template<class t> class Foo
{
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}

void Call()
{
cout<<"Bar::Call"<<endl;
}
};
public:
static Bar x;
t y;

};

template<class t> Foo<t>::Bar Foo<t>::x;

int main()
{
Foo<int> f;
Foo<int>::x.Call();

return 0;
}
/**************************/


g++ 4.0.2 throws the following error on that code (although g++ 2.95.3
accepts it):

test.cpp:25: error: expected constructor, destructor, or type conversion
before 'Foo'

Rennie


I believe you need to declare Foo<t>::Bar as a typename, like so:
template<class t> typename Foo<t>::Bar Foo<t>::x;

g++4.0.0 then accepts it.

-matt

Dec 30 '05 #6
Rennie deGraaf wrote:
I'm writing a class similar to this:

#include <iostream>
using namespace std;

template<class t> class Foo
{
private:
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}
};
static Bar x;
t y;
};

template<> Foo<int>::Bar Foo<int>::x;

int main()
{
return 0;
}

When executed, it should print "hello, world" to the console, which it
does when compiled with Sun's CC compiler. However, it does nothing
when compiled with GCC (versions 4.0.2 and older). GCC-compiled
versions produce the expected output when Foo is made into a normal,
rather than template, class, or when Foo::x is made into a pointer and
initialized with a dynamically-allocated object. Is this a bug in GCC,
or one of those poorly-specified edge cases in C++?

Rennie


We discussed a similar problem in this thread (see especially the posts
by James Kanze):

http://groups.google.com/group/comp....48a992ba350f2f

In essence, though GCC's is not the behavior you want, it is the more
standard-conformant compiler on this point, and Sun is incorrect. The
static member x should not be instantiated unless it is actually
referenced, which it is not in your sample program. You can force
instantiation on a conforming compiler by adding a trivial reference to
x somewhere, e.g. in the destructor for Foo<> with something like this:

~Foo() { (void)&x; }

Cheers! --M

Dec 30 '05 #7
Matteo wrote:
Rennie deGraaf wrote:
AD wrote:
I made slight modifications to your program and it worked.
a) I changed the initialisation of static member of template class Foo
as following:
"template<class t> Foo<t>::Bar Foo<t>::x;"
This initializes x for all typenames and not only int.
b) I added a function to class Bar and then used x in Foo to call this
function.
This caused x to be instantiated and Bar constructor was called. This
is explained by concept that template members are initialised at the
first point of use in program and not necessarily at the
definition/declaration. So mere declaration/definition of static member
of template class wont instantiate it.

Following is the changed program and it worked with g++ compiler.

#include <iostream>
using namespace std;

template<class t> class Foo
{
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}

void Call()
{
cout<<"Bar::Call"<<endl;
}
};
public:
static Bar x;
t y;

};

template<class t> Foo<t>::Bar Foo<t>::x;

int main()
{
Foo<int> f;
Foo<int>::x.Call();

return 0;
}
/**************************/


g++ 4.0.2 throws the following error on that code (although g++ 2.95.3
accepts it):

test.cpp:25: error: expected constructor, destructor, or type conversion
before 'Foo'

Rennie

I believe you need to declare Foo<t>::Bar as a typename, like so:
template<class t> typename Foo<t>::Bar Foo<t>::x;

g++4.0.0 then accepts it.

-matt


Okay, that works, but the whole reason I'm doing this is to use Bar as a
class constructor for the various instantiations of Foo<>. If I have to
explicitly define an instance of each instantiation of Foo<> and call a
method on each, then there's no point of using a class constructor at
all. It also makes my code a lot less extensible.

Does anyone know any better ways to write class contstructors for
template classes?

Rennie
Dec 30 '05 #8
mlimber wrote:
Rennie deGraaf wrote:
I'm writing a class similar to this:

#include <iostream>
using namespace std;

template<class t> class Foo
{
private:
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}
};
static Bar x;
t y;
};

template<> Foo<int>::Bar Foo<int>::x;

int main()
{
return 0;
}

When executed, it should print "hello, world" to the console, which it
does when compiled with Sun's CC compiler. However, it does nothing
when compiled with GCC (versions 4.0.2 and older). GCC-compiled
versions produce the expected output when Foo is made into a normal,
rather than template, class, or when Foo::x is made into a pointer and
initialized with a dynamically-allocated object. Is this a bug in GCC,
or one of those poorly-specified edge cases in C++?

Rennie


We discussed a similar problem in this thread (see especially the posts
by James Kanze):

http://groups.google.com/group/comp....48a992ba350f2f

In essence, though GCC's is not the behavior you want, it is the more
standard-conformant compiler on this point, and Sun is incorrect. The
static member x should not be instantiated unless it is actually
referenced, which it is not in your sample program. You can force
instantiation on a conforming compiler by adding a trivial reference to
x somewhere, e.g. in the destructor for Foo<> with something like this:

~Foo() { (void)&x; }

Cheers! --M


On second thought, I'm not sure that this is the same scenario since
you are explicitly specializing Foo.

M

Dec 30 '05 #9
Rennie deGraaf wrote:
Matteo wrote:
Rennie deGraaf wrote:
AD wrote:

I made slight modifications to your program and it worked.
a) I changed the initialisation of static member of template class Foo
as following:
"template<class t> Foo<t>::Bar Foo<t>::x;"
This initializes x for all typenames and not only int.
b) I added a function to class Bar and then used x in Foo to call this
function.
This caused x to be instantiated and Bar constructor was called. This
is explained by concept that template members are initialised at the
first point of use in program and not necessarily at the
definition/declaration. So mere declaration/definition of static member
of template class wont instantiate it.

Following is the changed program and it worked with g++ compiler.

#include <iostream>
using namespace std;

template<class t> class Foo
{
class Bar
{
public:
Bar()
{
cout << "hello, world" << endl;
}

void Call()
{
cout<<"Bar::Call"<<endl;
}
};
public:
static Bar x;
t y;

};

template<class t> Foo<t>::Bar Foo<t>::x;

int main()
{
Foo<int> f;
Foo<int>::x.Call();

return 0;
}
/**************************/
g++ 4.0.2 throws the following error on that code (although g++ 2.95.3
accepts it):

test.cpp:25: error: expected constructor, destructor, or type conversion
before 'Foo'

Rennie

I believe you need to declare Foo<t>::Bar as a typename, like so:
template<class t> typename Foo<t>::Bar Foo<t>::x;

g++4.0.0 then accepts it.

-matt


Okay, that works, but the whole reason I'm doing this is to use Bar as a
class constructor for the various instantiations of Foo<>. If I have to
explicitly define an instance of each instantiation of Foo<> and call a
method on each, then there's no point of using a class constructor at
all. It also makes my code a lot less extensible.

Does anyone know any better ways to write class contstructors for
template classes?

Rennie


See my other response. It may have what you need.

Cheers! --M

Jan 3 '06 #10

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

Similar topics

25
by: John Harrison | last post by:
This code fails to compile on Comeau C++ and VC++ 7.1 (with language extensions disabled) template <class T> struct B { T b; }; template <class T>
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>...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
4
by: Skybuck Flying | last post by:
Hello, I have a question about the C language. This piece of code is from the glibc library. My question is about the static struct The question is....
5
by: Dale | last post by:
Is it possible to declare a method of an interface as static? For instance, can I create an interface that defines a static method and 2 instance methods?
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
10
by: Jeffrey | last post by:
My understanding is that if you write class X { int y; static int z; }; then you've defined (and declared) X and y, but you have only declared (and not defined) z. If you'd like to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...

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.