473,372 Members | 1,039 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,372 software developers and data experts.

template static member

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 void UseMap (const std::string &key, int value)
{
std::cout << gName << std::endl;
gMap[key] = value;
}

private:
static const std::string gName;
static std::map<std::string, intgMap;
};

class DerivedT : public BaseT<DerivedT>
{
public:
// Some code soon or late....
};

// Now the specialization for BaseT<DerivedT>

// This one work fine
template<>
const std::string BaseT<DerivedT>::gName("Derived");

// This one gives me a linkage error:
// In function BaseT<DerivedT>::UseMap(...):
// undefined reference to BaseT<DerivedT>::gMap
template<>
std::map<std::string, intBaseT<DerivedT>::gMap;

int main (int argc, char** argv)
{
DerivedT a;
a.UseMap ("test", 4);
}
----

So, i was wandering, if there is a special way to declare a static
member (which use the std::map template) of a template.

Later everything will be split up into several files, but for now i'm
using a single c++ source file to try to solve this problem. I've
tried with g++ 4.3.0 (x86_64, RH FC9) and a arm-linux-g++ 3.4, both
gives same results.

Thanks,
Christian
Sep 27 '08 #1
5 3747
chgans wrote:
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 void UseMap (const std::string &key, int value)
{
std::cout << gName << std::endl;
gMap[key] = value;
}

private:
static const std::string gName;
static std::map<std::string, intgMap;
};

class DerivedT : public BaseT<DerivedT>
{
public:
// Some code soon or late....
};

// Now the specialization for BaseT<DerivedT>

// This one work fine
template<>
const std::string BaseT<DerivedT>::gName("Derived");

// This one gives me a linkage error:
// In function BaseT<DerivedT>::UseMap(...):
// undefined reference to BaseT<DerivedT>::gMap
template<>
std::map<std::string, intBaseT<DerivedT>::gMap;
Why try and specialise?

template <typename T>
std::map<std::string, intBaseT<T>::gMap;

Will be fine.

--
Ian Collins.
Sep 27 '08 #2
On Sep 27, 3:49 pm, chgans <chg...@googlemail.comwrote:
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 void UseMap (const std::string &key, int value)
{
std::cout << gName << std::endl;
gMap[key] = value;
}

private:
static const std::string gName;
static std::map<std::string, intgMap;

};

class DerivedT : public BaseT<DerivedT>
{
public:
// Some code soon or late....

};

// Now the specialization for BaseT<DerivedT>

// This one work fine
template<>
const std::string BaseT<DerivedT>::gName("Derived");

// This one gives me a linkage error:
// In function BaseT<DerivedT>::UseMap(...):
// undefined reference to BaseT<DerivedT>::gMap
template<>
std::map<std::string, intBaseT<DerivedT>::gMap;

int main (int argc, char** argv)
{
DerivedT a;
a.UseMap ("test", 4);}

----

So, i was wandering, if there is a special way to declare a static
member (which use the std::map template) of a template.

Later everything will be split up into several files, but for now i'm
using a single c++ source file to try to solve this problem. I've
tried with g++ 4.3.0 (x86_64, RH FC9) and a arm-linux-g++ 3.4, both
gives same results.

Thanks,
Christian
It seems that if you specialize a static member you can't do it with a
default constructor. You can either write:

template< class T >
std::map<std::string, intBaseT< T >::gMap;

or:

template<>
std::map<std::string, intBaseT< DerivedT >::gMap( anotherMap );

However, I tested it using only one compiler (g++ 4.1.2) and I did not
look into the Standard so I am not sure that it is what it requires.

Example:

template< class T >
struct A
{
static void
push( T const & v )
{
vec.push_back( v );
}

static std::vector< T vec;
};

template< class T >
std::vector< T A< T >::vec;

/*
template<>
std::vector< double A< double >::vec; // won't work
*/

template<>
std::vector< double A< double >::vec( 10 ); // ok

int
main()
{
A< int >::push( 10 );
A< double >::push( 10.0 );
}

--
Szymon
Sep 27 '08 #3


chgans wrote:
Hi all,
// Now the specialization for BaseT<DerivedT>

// This one work fine
template<>
const std::string BaseT<DerivedT>::gName("Derived");

// This one gives me a linkage error:
// In function BaseT<DerivedT>::UseMap(...):
// undefined reference to BaseT<DerivedT>::gMap
template<>
std::map<std::string, intBaseT<DerivedT>::gMap;
change it into:
template<>
std::map<std::string, intBaseT<DerivedT>::gMap =
std::map<std::string, int>();

to give gMap a memory space.

hth,
Jim
>
int main (int argc, char** argv)
{
DerivedT a;
a.UseMap ("test", 4);
}
----

So, i was wandering, if there is a special way to declare a static
member (which use the std::map template) of a template.

Later everything will be split up into several files, but for now i'm
using a single c++ source file to try to solve this problem. I've
tried with g++ 4.3.0 (x86_64, RH FC9) and a arm-linux-g++ 3.4, both
gives same results.

Thanks,
Christian
Sep 28 '08 #4
On Sep 27, 10:45 pm, SzymonWlodar...@gmail.com wrote:
On Sep 27, 3:49 pm, chgans <chg...@googlemail.comwrote:
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 void UseMap (const std::string &key, int value)
{
std::cout << gName << std::endl;
gMap[key] = value;
}
private:
static const std::string gName;
static std::map<std::string, intgMap;
};
class DerivedT : public BaseT<DerivedT>
{
public:
// Some code soon or late....
};
// Now the specialization for BaseT<DerivedT>
// This one work fine
template<>
const std::string BaseT<DerivedT>::gName("Derived");
// This one gives me a linkage error:
// In function BaseT<DerivedT>::UseMap(...):
// undefined reference to BaseT<DerivedT>::gMap
template<>
std::map<std::string, intBaseT<DerivedT>::gMap;
int main (int argc, char** argv)
{
DerivedT a;
a.UseMap ("test", 4);
}
----
So, i was wandering, if there is a special way to declare a
static member (which use the std::map template) of a
template.
It seems that if you specialize a static member you can't do
it with a default constructor. You can either write:
template< class T >
std::map<std::string, intBaseT< T >::gMap;
or:
template<>
std::map<std::string, intBaseT< DerivedT >::gMap( anotherMap );
However, I tested it using only one compiler (g++ 4.1.2) and I
did not look into the Standard so I am not sure that it is
what it requires.
Note that a specialization is not a template, but rather a
declaration or definition of a non-template entity with a name
that looks like a template instantiation, to be used instead of
the instantiation.

Givan that, the basic problem in this is that without an
initializer, the compiler interprets the static member
specialization as a declaration, not a definition, and since
it's not a template, you need a definition (in one, and only
one, translation unit). See §14.7.3/15:

An explicit specialization of a static data member of a
template is a definition if the declaration includes an
initializer; otherwise, it is a declaration. [Note:
there is no syntax for the definition of a static data
member of a template which requires default
initialization.

template<X Q<int>::x ;

This is a declaration regardless of whether X can be
default initialized.]

Note the note!

Note too that formally, you can only provide a single
definition, which means that the definition should be in a
source file, and not in a header; i.e.:

In the header:
template< class T >
std::map< std::string, int BaseT< DerivedT >::gMap ;

and then in one and only one source file (which includes the
header):
template< class T >
std::map< std::string, int BaseT< DerivedT >::gMap(
std::map< std::string, int BaseT< DerivedT >() ) ;
(Luckily, we can use the copy constructor in this case.)

--
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
Sep 28 '08 #5
On Sep 28, 8:23*am, James Kanze <james.ka...@gmail.comwrote:
On Sep 27, 10:45 pm, SzymonWlodar...@gmail.com wrote:
On Sep 27, 3:49 pm, chgans <chg...@googlemail.comwrote:
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 void UseMap (const std::string &key, int value)
* {
* * std::cout << gName << std::endl;
* * gMap[key] = value;
* }
private:
* static const std::string gName;
* static std::map<std::string, intgMap;
};
class DerivedT : public BaseT<DerivedT>
{
public:
* *// Some code soon or late....
};
// Now the specialization for BaseT<DerivedT>
// This one work fine
template<>
const std::string BaseT<DerivedT>::gName("Derived");
// This one gives me a linkage error:
// In function BaseT<DerivedT>::UseMap(...):
// undefined reference to BaseT<DerivedT>::gMap
template<>
std::map<std::string, intBaseT<DerivedT>::gMap;
int main (int argc, char** argv)
{
* DerivedT a;
* a.UseMap ("test", 4);
}
----
So, i was wandering, if there is a special way to declare a
static member (which use the std::map template) of a
template.
It seems that if you specialize a static member you can't do
it with a default constructor. You can either write:
template< class T >
std::map<std::string, intBaseT< T >::gMap;
or:
template<>
std::map<std::string, intBaseT< DerivedT >::gMap( anotherMap );
However, I tested it using only one compiler (g++ 4.1.2) and I
did not look into the Standard so I am not sure that it is
what it requires.

Note that a specialization is not a template, but rather a
declaration or definition of a non-template entity with a name
that looks like a template instantiation, to be used instead of
the instantiation.

Givan that, the basic problem in this is that without an
initializer, the compiler interprets the static member
specialization as a declaration, not a definition, and since
it's not a template, you need a definition (in one, and only
one, translation unit). *See §14.7.3/15:

* * An explicit specialization of a static data member of a
* * template is a definition if the declaration includes an
* * initializer; otherwise, it is a declaration. [Note:
* * there is no syntax for the definition of a static data
* * member of a template which requires default
* * initialization.

* * * * template<X Q<int>::x ;

* * This is a declaration regardless of whether X can be
* * default initialized.]

Note the note!

Note too that formally, you can only provide a single
definition, which means that the definition should be in a
source file, and not in a header; i.e.:

In the header:
* * template< class T >
* * std::map< std::string, int BaseT< DerivedT >::gMap ;

and then in one and only one source file (which includes the
header):
* * template< class T >
* * std::map< std::string, int BaseT< DerivedT >::gMap(
* * * * * * std::map< std::string, int BaseT< DerivedT >() ) ;
(Luckily, we can use the copy constructor in this case.)
Thank you all,
Problem solved now! :)

>
--
James Kanze (GABI Software) * * * * * * email:james.ka...@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
Sep 28 '08 #6

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

Similar topics

0
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up...
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>...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
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...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
1
by: Frederiek | last post by:
Hi, When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. Does that mean that the address of...
5
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...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.