This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/5018: cannot define template member outside class declarations
- From: rmaddox at isicns dot com
- To: gcc-gnats at gcc dot gnu dot org
- Date: 5 Dec 2001 13:55:43 -0000
- Subject: c++/5018: cannot define template member outside class declarations
- Reply-to: rmaddox at isicns dot com
>Number: 5018
>Category: c++
>Synopsis: cannot define template member outside class declarations
>Confidential: no
>Severity: critical
>Priority: medium
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Wed Dec 05 05:56:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator: Randy Maddox
>Release: 3.0.2
>Organization:
>Environment:
Windows 2000
>Description:
Given a template class with a template member it is not possible to define the template member outside of the class declarations. Defining the template member inside the class declarations works as expected. The syntax used appears legal as per the Standard and the error reported seems like a mistake since the compiler message states that the definition is a "partial specialization". Sample source (short) to duplicate problem and output from compiler is given in "How-To-Repeat".
>How-To-Repeat:
- This compiles fine and works as expected:
// test driver for template class template member
#include <iostream>
template <typename T1> class Test
{
public:
Test(const T1 & privateValue)
: privateValue_(privateValue)
{ }
template <typename T2>
void Write(const T2 & value)
{
std::cout << "Write() value is: " << value << std::endl;
std::cout << "privateValue is: " << privateValue_ << std::endl
<< std::endl;
}
private:
const T1 privateValue_;
};
int main()
{
std::cout << "Start of test driver" << std::endl << std::endl;
Test<double> test(3.3);
test.Write("string value");
test.Write(4);
std::cout << std::endl << "End of test driver" << std::endl;
}
// end of this file
However, if I try to move the definition of Write() outside of the class declarations as shown here:
// test driver for template class template member
#include <iostream>
template <typename T1> class Test
{
public:
Test(const T1 & privateValue)
: privateValue_(privateValue)
{ }
template <typename T2>
void Write(const T2 & value);
private:
const T1 privateValue_;
};
template <typename T1>
template <typename T2>
void Test<T1>::Write<T2>(const T2 & value)
{
std::cout << "Write() value is: " << value << std::endl;
std::cout << "privateValue is: " << privateValue_ << std::endl
<< std::endl;
}
int main()
{
std::cout << "Start of test driver" << std::endl << std::endl;
Test<double> test(3.3);
test.Write("string value");
test.Write(4);
std::cout << std::endl << "End of test driver" << std::endl;
}
// end of this file
Then I get the following compile error:
> gxx -Wall test2.cpp
test2.cpp:25: partial specialization `Write<T2>' of function template
Since, as far as I can tell from the Standard, this looks like the correct syntax why do I get this error? Is this a bug? An unsupported feature? Or do I just not actually understand the syntax?
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: