Template specialization bug(s)
Jon Lennard
jlennard@dsg.com
Wed Sep 6 08:56:00 GMT 2000
I encountered the following when specializing a template running
version 2.95.2 on Solaris using "g++ template.cpp -o template".
The source is extremely short, so I included it below.
The problems arise when defining the specialization outside of the
declaration, i.e.
template<>
TemplateTest<char *>::TemplateTest(char *attr)
: attr_(attr)
{
std::cout << "in specialization" << std::endl;
}
This causes:
template.cpp:30: template-id `TemplateTest<>' for
`test::TemplateTest<char *>::TemplateTest(char *)' does not match any
template declaration
template.cpp:30: syntax error before `:'
I accidentally changed "template<>" to "template".
This causes:
template.cpp:30: Internal compiler error.
template.cpp:30: Please submit a full bug report.
template.cpp:30: See
<URL: http://www.gnu.org/software/gcc/faq.html#bugreport > for instructions.
If I remove "template<>" entirely, everything compiles correctly.
Other compilers compile whether the "template<>" is present or not.
Which behavior is called for by the standard?
Jon Lennard
jlennard@dsg.com
---------------------------beginning of source------------------------
#include <iostream>
namespace test
{
template<class Type>
class TemplateTest
{
public:
TemplateTest(Type attr);
Type attr_;
};
template<>
class TemplateTest<char *>
{
public:
TemplateTest(char *attr);
char *attr_;
};
template<class Type>
TemplateTest<Type>::TemplateTest(Type attr)
: attr_(attr)
{
std::cout << "in general" << std::endl;
}
template<>
TemplateTest<char *>::TemplateTest(char *attr)
: attr_(attr)
{
std::cout << "in specialization" << std::endl;
}
}
int main()
{
char *str = "test";
test::TemplateTest<double> dtest(2.0);
test::TemplateTest<char *> ctest(str);
std::cout << dtest.attr_ << std::endl;
std::cout << ctest.attr_ << std::endl;
}
More information about the Gcc-bugs
mailing list