This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/18449] New: Problems when using function overload&templates across namespaces.
- From: "Jean-Paul dot Chaput at lip6 dot fr" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 12 Nov 2004 11:00:01 -0000
- Subject: [Bug c++/18449] New: Problems when using function overload&templates across namespaces.
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Hi there,
My apologies for not filling target triplet & build-triplet, I uses the stock
Fedora core 2 gcc-c++ package (3.3.3-7) with all packages updates.
Here is a self-contained source file (needs nothing but STL) which show the
problem. I have three namespaces (NS1, NS2 & NS3) and a GetString() function
which have overload for POD and non-POD types in NS1 & NS2. If there are
overload of GetString() for POD types in NS3, the overload of POD types
from other namesspaces seems to be not taken in account.
Anyway, thanks for developping that great compiler.
---- Source -------- Source -------- Source -------- Source --------
# include <string>
# include <iostream>
# include <iomanip>
# include <sstream>
using namespace std;
// Characterics common to all tests :
//
// In namespace NS1 :
// - GetString for class type "Int&".
// - GetString for POD type "Char".
//
// In namespace NS2 :
// - GetString for class type "Long&".
// - NO GetString for any POD type.
# define CHECK_1 0
// CHECK_1 :
//
// In namespace NS1 :
// - GetString template for "Data*".
//
// In namespace NS3 :
// - No GetString of any kind.
//
// Result :
// - Works OK for types from NS1 & NS2.
// - Pointer template OK for types from NS1 & NS2.
# if CHECK_1
# define NS3_OVERLOAD_CLASS 0
# define NS3_PODIMPORT 0
# define NS1_TEMPLATE 1
# endif
# if CHECK_2
# define NS3_OVERLOAD_CLASS 1
# define NS3_PODIMPORT 0
# define NS1_TEMPLATE 0
# endif
# define CHECK_3 0
// CHECK_3 :
//
// In namespace NS3 :
// - GetString overload for class type "Char&".
// - GetString "manual" importation of NS1::GetString(float).
//
// Result :
// - Works OK for class types from NS1, NS2 & NS3.
// - Uses manual importation for POD type float.
// -
# if CHECK_3
# define NS3_OVERLOAD_CLASS 1
# define NS3_PODIMPORT 1
# define NS1_TEMPLATE 0
# endif
# define CHECK_4 0
// CHECK_4 :
//
// In namespace NS1 :
// - GetString template for "Data*".
//
// In namespace NS3 :
// - GetString overload for class type "Char&".
// - GetString "manual" importation of NS1::GetString(float).
//
// Result :
// - Works OK for class types from NS1, NS2 & NS3.
// - Uses manual importation for POD type float.
// - Template seems to be ignored for types coming from
// namespaces other than NS1.
# if CHECK_4
# define NS3_OVERLOAD_CLASS 1
# define NS3_PODIMPORT 1
# define NS1_TEMPLATE 1
# endif
# define CHECK_5 1
// CHECK_5 :
//
// In namespace NS3 :
// - NO overload for of kind.
// - GetString "manual" importation of NS1::GetString(float).
//
// Result :
// - Works OK : overload is correctly handled for POD type float.
// (uses first the manual importation in NS3).
// - As in CHECK_4, Template seems to be ignored for types coming
// from namespaces other than NS1.
# if CHECK_5
# define NS3_OVERLOAD_CLASS 0
# define NS3_PODIMPORT 1
# define NS1_TEMPLATE 1
# endif
namespace NS1 {
class Int {
private:
int _value;
public:
explicit Int ( int value ) : _value(value) { };
int GetValue () const { return _value; };
};
string GetString ( const Int& data )
{
cout << setw(79) << right
<< "NS1::Getstring(const Int&)" << endl;
ostringstream os;
os << data.GetValue();
return os.str();
}
string GetString ( const float f )
{
cout << setw(79) << right
<< "NS1::Getstring(const float)" << endl;
ostringstream os;
os << f;
return os.str();
}
# if NS1_TEMPLATE
template<class Data>
string GetString ( const Data* data ) {
cout << setw(79) << right
<< "template<class Data>NS1::Getstring(const Data*)"
<< endl;
return (data) ? GetString(*data) : "NULL";
}
# endif
}
namespace NS2 {
using namespace NS1;
class Long {
private:
long _value;
public:
explicit Long ( long value ) : _value(value) { };
long GetValue () const { return _value; };
};
string GetString ( const Long& data )
{
cout << setw(79) << right
<< "NS2::Getstring(const Long&)" << endl;
ostringstream os;
os << data.GetValue();
return os.str();
}
}
namespace NS3 {
using namespace NS2;
# if NS3_OVERLOAD_CLASS
class Char {
private:
const char* _value;
public:
explicit Char ( const char* value ) : _value(value) { };
const char* GetValue () const { return _value; };
};
string GetString ( const Char& data )
{
cout << setw(79) << right
<< "NS3::Getstring(const Char&)"
<< endl;
ostringstream os;
os << data.GetValue();
return os.str();
}
# endif
# if NS3_PODIMPORT
inline string GetString ( const float f ) {
cout << setw(79) << right
<< "inline NS3::GetString(const float)"
<< endl;
return NS1::GetString(f);
}
# endif
void TheTest ( ostream& o )
{
Int i(1);
float j = 2.0;
Long k(3);
# if NS3_OVERLOAD_CLASS
Char l("4");
# endif
o << "Checking Simple Overload :" << endl;
o << " GetString(Int) := " << GetString(i) << endl;
o << " GetString(float) := " << GetString(j) << endl;
o << " GetString(Long) := " << GetString(k) << endl;
# if NS3_OVERLOAD_CLASS
o << " GetString(Char) := " << GetString(l) << endl;
# endif
# if NS1_TEMPLATE
o << "Checking Overload+Template :" << endl;
o << " GetString(Int*) := " << GetString(&i) << endl;
o << " GetString(float*) := " << GetString(&j) << endl;
o << " GetString(Long*) := " << GetString(&k) << endl;
# if NS3_OVERLOAD_CLASS
o << " GetString(Char*) := " << GetString(&l) << endl;
# endif
# endif
}
}
using namespace NS3;
int main ( int argc, char* argv[] )
{
TheTest ( cout );
return 0;
}
--
Summary: Problems when using function overload&templates across
namespaces.
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: Jean-Paul dot Chaput at lip6 dot fr
CC: gcc-bugs at gcc dot gnu dot org
GCC host triplet: i386-redhat-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18449