473,246 Members | 1,599 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,246 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 1482
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.