This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/15629] New: Function overloading doesn't happen when I use friend function templates in class templates.
- From: "ywhu at nmi dot iii dot org dot tw" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 24 May 2004 08:18:07 -0000
- Subject: [Bug c++/15629] New: Function overloading doesn't happen when I use friend function templates in class templates.
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Hi,
When I use friend function templates in a class template, the function
overloading doesn't happend.
The following is the source.
==============================================================
#include <iostream>
template<int a, int b>
class T;
template<int a, int b>
void func(T<a, b> const * const t)
{
std::cerr << "T<a, b>" << std::endl;
}
template<int a, int b>
class T
{
private:
friend void func<a, b>(T<a, b> const * const t);
friend void func<a>(T<a, 3> const * const t);
public:
void ttt() const;
};
template<int a>
void
func(T<a, 3> const * const t)
{
std::cerr << "T<a, 3>" << std::endl;
}
template<int a, int b>
void
T<a, b>::ttt() const
{
func(this);
}
int
main()
{
T<2, 3> t;
t.ttt();
return 0;
}
==============================================================
The error messages from gcc-3.2 is as following:
==============================================================
eee.cpp: In member function `void T<a, b>::ttt() const [with int a =
2, int b =
3]':
eee.cpp:52: instantiated from here
eee.cpp:44: call of overloaded `func(const T<2, 3>* const)' is
ambiguous
eee.cpp:8: candidates are: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]
eee.cpp:8: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]
==============================================================
However, when I use gcc-3.2, gcc-3.3, gcc-3.4 to compile my code, the
result is strange.
Here is my result:
1) gcc-3.2: can _not_ compile, the error message is copied and pasted
above.
2) gcc-3.3: can compile, but the result is
T<a, b>
[but I think it should be T<a, 3>]
3) gcc-3.4: can compile, but the result is
T<a, b>
[but I think it should be T<a, 3>]
< NOTE >
When I use 'struct' rather than 'class', and remove the 'friend'
declarations,
the function overloading works fine !
=============================================================
Question 2:
=============================================================
I added one more friend function declaration, and now, even gcc-3.3
doesn't compile my code.
The source code is as following:
==============================================================
#include <iostream>
template<int a, int b>
class T;
template<int a, int b>
void func(T<a, b> const * const t)
{
std::cerr << "T<a, b>" << std::endl;
}
template<int a, int b>
class T
{
private:
friend void func<a, b>(T<a, b> const * const t);
friend void func<a>(T<a, 3> const * const t);
friend void func<b>(T<3, b> const * const t); <-- add this -->
public:
void ttt() const;
};
template<int a>
void
func(T<a, 3> const * const t)
{
std::cerr << "T<a, 3>" << std::endl;
}
template<int b> <-- add this definition -->
void
func(T<3, b> const * const t)
{
std::cerr << "T<3, b>" << std::endl;
}
template<int a, int b>
void
T<a, b>::ttt() const
{
func(this);
}
int
main()
{
T<2, 3> t;
t.ttt();
return 0;
}
==============================================================
I can _not_ compile the code using gcc-3.2, and the error message for
gcc-3.2 is
like the one I posted above:
==============================================================
eee.cpp: In instantiation of `T<2, 3>':
eee.cpp:50: instantiated from here
eee.cpp:19: ambiguous template specialization `func<3>' for `void
func(const
T<3, 3>*)'
eee.cpp: In member function `void T<a, b>::ttt() const [with int a =
2, int b =
3]':
eee.cpp:52: instantiated from here
eee.cpp:44: call of overloaded `func(const T<2, 3>* const)' is
ambiguous
eee.cpp:8: candidates are: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]
eee.cpp:8: void func(const T<a, b>*) [with int a = 2,
int b =
3]
eee.cpp:29: void func(const T<a, 3>*) [with int a = 2]
==============================================================
Again, the following is the result when I compile it using gcc-3.2,
gcc-3.3 and gcc-3.4:
1) gcc-3.2: can _not_ compile, the error message posted above.
2) gcc-3.3: can _not_ compile, the error message just like the one
posted for gcc-3.2.
3) gcc-3.4: can compile, but the result is
T<a, b>
[but I think it should be T<a, 3>]
==============================================================
My environment:
1) Debian unstable
2) gcc-3.4.0 (Debian 20040516)
3) gcc-3.3.3 (Debian 20040422)
4) gcc-3.2.3
5) Command line options: none
6)
--
Summary: Function overloading doesn't happen when I use friend
function templates in class templates.
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: critical
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: ywhu at nmi dot iii dot org dot tw
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15629
- Follow-Ups:
- [Bug c++/15629] Function templates, overloads, and friend name injection
- From: bangerth at dealii dot org
- [Bug c++/15629] Function templates, overloads, and friend name injection
- From: pinskia at gcc dot gnu dot org
- [Bug c++/15629] [3.3/3.4/3.5 Regressions] Function templates, overloads, and friend name injection
- From: giovannibajo at libero dot it
- [Bug c++/15629] [3.3/3.4/3.5 Regressions] Function templates, overloads, and friend name injection
- From: mmitchel at gcc dot gnu dot org
- [Bug c++/15629] [3.3/3.4/3.5 Regressions] Function templates, overloads, and friend name injection
- From: cvs-commit at gcc dot gnu dot org
- [Bug c++/15629] [3.3/3.4/3.5 Regressions] Function templates, overloads, and friend name injection
- From: cvs-commit at gcc dot gnu dot org
- [Bug c++/15629] [3.3/3.4/3.5 Regressions] Function templates, overloads, and friend name injection
- From: mmitchel at gcc dot gnu dot org