This is GCC Bugzilla
This is GCC Bugzilla Version 2.20+
View Bug Activity | Format For Printing | Clone This Bug
The following code give this error (compiles fine in 3.3.3): I don't know if I need to fix my code, or if it is a bug in 3.4.1. test.cpp:10: error: too few template-parameter-lists test.cpp:10: error: expected `,' or `;' before '=' token template <class T> class MyType { public: static char *name; }; class Sample { }; MyType<Sample> list; char *MyType<Sample>::name = "Hello\n"; int main(int argc, char **argv) { } gcc -v -save-temps test.cpp Reading specs from /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev67- dec-osf5.1/3.4.1/specs Configured with: /usr/global/src/gnu/gcc/gcc-3.4.1/configure -- prefix=/usr/local/tools/gnu/gcc/3.4.1_tru64_5 Thread model: posix gcc version 3.4.1 /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../libexec/gcc/alphaev67- dec-osf5.1/3.4.1/cc1plus -E -quiet -v - iprefix /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev67- dec-osf5.1/3.4.1/ test.cpp -mcpu=ev67 -o test.ii ignoring nonexistent directory "/usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev 67-dec-osf5.1/3.4.1/../../../../alphaev67-dec-osf5.1/include" ignoring duplicate directory "/usr/local/tools/gnu/gcc/3.4.1_tru64_5/lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../include/c++/3.4.1" ignoring duplicate directory "/usr/local/tools/gnu/gcc/3.4.1_tru64_5/lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../include/c++/3.4.1/alphaev67-dec-osf5.1" ignoring duplicate directory "/usr/local/tools/gnu/gcc/3.4.1_tru64_5/lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../include/c++/3.4.1/backward" ignoring duplicate directory "/usr/local/tools/gnu/gcc/3.4.1_tru64_5/lib/gcc/alphaev67-dec- osf5.1/3.4.1/include" ignoring nonexistent directory "/usr/local/tools/gnu/gcc/3.4.1_tru64_5/lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../alphaev67-dec-osf5.1/include" #include "..." search starts here: #include <...> search starts here: /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../include/c++/3.4.1 /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../include/c++/3.4.1/alphaev67-dec-osf5.1 /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev67-dec- osf5.1/3.4.1/../../../../include/c++/3.4.1/backward /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../lib/gcc/alphaev67-dec- osf5.1/3.4.1/include /usr/local/include /usr/local/tools/gnu/gcc/3.4.1_tru64_5/include /usr/include End of search list. /usr/local/tools/openSource/gcc/3.4.1_tru64_5/bin/../libexec/gcc/alphaev67- dec-osf5.1/3.4.1/cc1plus -fpreprocessed test.ii -quiet -dumpbase test.cpp - mcpu=ev67 -auxbase test -version -o test.s GNU C++ version 3.4.1 (alphaev67-dec-osf5.1) compiled by GNU C version 3.3.3. GGC heuristics: --param ggc-min-expand=57 --param ggc-min-heapsize=51200
*** Bug 17446 has been marked as a duplicate of this bug. ***
You want: template<> MyType<Sample> list; char *MyType<Sample>::name = "Hello\n";
(In reply to comment #2) > You want: > template<> > MyType<Sample> list; > char *MyType<Sample>::name = "Hello\n"; The following code seg faults with g++ 3.4.3. If I remove the "template <>", it won't compile. On g++ 3.3, without the "template <>" it is happy and prints "goodbye". With the "template <>" it also seg faults. I'd like to know if this is a bug, or my inability to figure out the correct syntax. thanks #include <cstdio> #include <map> template <class T> class MyType { public: static std::map<char*,char*> m_map; }; class Sample { }; template class MyType<Sample>; template <> std::map<char*,char*> MyType<Sample>::m_map; int main(int argc, char **argv) { MyType<Sample>::m_map["hello"]="goodbye"; printf("%s\n",MyType<Sample>::m_map["hello"]); }
A segfault in GCC is always a bug, even if the code is wrong. Would you please open a new bugreport about it?
(In reply to comment #4) > A segfault in GCC is always a bug, even if the code is wrong. Would you please > open a new bugreport about it? Sorry, I wasn't clear. The binary seg faults, not the compiler.
This code has at least two bugs: template class MyType<Sample>; template <> std::map<char*,char*> MyType<Sample>::m_map; First, the instantiation must come *after* the definition of the static member. Second, the definition you thought you have written is in fact only the declaration of an explicit specialization of that member. It needs an initializer. You need to write template <> std::map<char*,char*> MyType<Sample>::m_map = std::map<char*,char*>(); W.
(In reply to comment #6) > This code has at least two bugs: > > template class MyType<Sample>; > template <> std::map<char*,char*> MyType<Sample>::m_map; > > First, the instantiation must come *after* the definition of the static > member. > Second, the definition you thought you have written is in fact only the > declaration of an explicit specialization of that member. It needs an > initializer. You need to write > > template <> std::map<char*,char*> MyType<Sample>::m_map > = std::map<char*,char*>(); > > W. > (In reply to comment #6) > This code has at least two bugs: > > template class MyType<Sample>; > template <> std::map<char*,char*> MyType<Sample>::m_map; > > First, the instantiation must come *after* the definition of the static > member. > Second, the definition you thought you have written is in fact only the > declaration of an explicit specialization of that member. It needs an > initializer. You need to write > > template <> std::map<char*,char*> MyType<Sample>::m_map > = std::map<char*,char*>(); > > W. > Great, thanks! If someone could put this on the g++ 3.4.3 changes page, that would probably save people quite a bit of time. http://www.gnu.org/software/gcc/gcc-3.4/changes.html
Why? This code as always wrong. It has nothing to do with gcc3.4.x. W.
*** Bug 21200 has been marked as a duplicate of this bug. ***
*** Bug 11585 has been marked as a duplicate of this bug. ***
*** Bug 14891 has been marked as a duplicate of this bug. ***
*** Bug 11930 has been marked as a duplicate of this bug. ***
From Wolfgang Bangerth: You need to write: template <> std::map<char*,char*> MyType<Sample>::m_map = std::map<char*,char*>(); My problem with this is that it requires that the static member support the copy constructor. In my case, the static member is something that cannot be copied, so I put an explicit copy constructor into the private section of my template class, which makes the above statement generate an error that the copy constructor is private. How do I instantiate this static member? It's default constructor does all the initialization I want, and I don't want that initialization done twice.
I believe you can't.