473,563 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with overloading function templates

Hi,

I have a problem, that boils down to the following code:

#include <iostream>
#include <typeinfo>

class Test1 {};
class Test2 {};
class Test3 {};

template< typename T >
struct Cont { T t; };

template< typename T >
class TT { public: TT(T) {} };

struct X {

template< template<typena me> class TT, typename T >
static void f(const T& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT >
static void f(const Test3& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f<TT>( c.t ); // <== error here
}

};

int main()
{
Cont<Test1> c1;
Cont<Test2> c2;
Cont<Test3> c3;

X::f< TT, Test1 >(c1);
X::f< TT, Test2 >(c2);
X::f< TT, Test3 >(c3);

return 0;
}

For this Comeau and CW9 report (in the line marked
"error here") that the call is ambigous. (VC7.1
accepts the code, BTW.)
First, I don't see what's wrong, but overloading
rules, especially in conjunctions with templates,
are too complicated for me to remember them anyway.
Second, I need a solution. I'm sure I would get it
to work if I would use a class template and partial
specialize it. However, the original code is long
and complex enough without yet another helper class
template...
I would hope that, once the problem is known, a
simpler way could be found. AFAIK, function template
overloading _is_ possible, after all.

TIA,

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #1
7 1989
On Fri, 30 Apr 2004 16:28:28 +0200, "Hendrik Schober" <Sp******@gmx.d e>
wrote:
Hi,

I have a problem, that boils down to the following code:

#include <iostream>
#include <typeinfo>

class Test1 {};
class Test2 {};
class Test3 {};

template< typename T >
struct Cont { T t; };

template< typename T >
class TT { public: TT(T) {} };

struct X {

template< template<typena me> class TT, typename T >
static void f(const T& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT >
static void f(const Test3& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f<TT>( c.t ); // <== error here
}

};

int main()
{
Cont<Test1> c1;
Cont<Test2> c2;
Cont<Test3> c3;

X::f< TT, Test1 >(c1);
X::f< TT, Test2 >(c2);
X::f< TT, Test3 >(c3);

return 0;
}

For this Comeau and CW9 report (in the line marked
"error here") that the call is ambigous. (VC7.1
accepts the code, BTW.)
First, I don't see what's wrong, but overloading
rules, especially in conjunctions with templates,
are too complicated for me to remember them anyway.
Second, I need a solution. I'm sure I would get it
to work if I would use a class template and partial
specialize it. However, the original code is long
and complex enough without yet another helper class
template...
I would hope that, once the problem is known, a
simpler way could be found. AFAIK, function template
overloading _is_ possible, after all.
Perhaps you've OVER-simplified, because it is not clear to my why you have
the template-template parameter there in your templates. I'm not sure, but
if it were used perhaps there might be more of a handle to be had on how to
disambiguate your calls. If you don't really need that
template-template-parameter, then your class can just be this:

struct X {

// template< template<typena me> class TT, typename T >
template<typena me T >
static void f(const T& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}
// template< template<typena me> class TT >
static void f(const Test3& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f( c.t ); // <== error here
}

};

-leor

TIA,

Schobi


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman <le**@bdsoft.co m> wrote:
[...]
Perhaps you've OVER-simplified, because it is not clear to my why you have
the template-template parameter there in your templates.
Yes, that's due to the simplification.
In the real code, the template template
arg is used to pick some algorithms that
are to be applied. They are vital to
these functions. :(
I'm not sure, but
if it were used perhaps there might be more of a handle to be had on how to
disambiguate your calls. If you don't really need that
template-template-parameter, then your class can just be this:

struct X {

// template< template<typena me> class TT, typename T >
template<typena me T >
static void f(const T& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}
// template< template<typena me> class TT >
static void f(const Test3& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f( c.t ); // <== error here
}

};
I am surprised. Why would the other template arg
cause the call to be ambiguos?
-leor


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #3
On Fri, 30 Apr 2004 16:53:59 +0200, "Hendrik Schober" <Sp******@gmx.d e>
wrote:
I'm not sure, but
if it were used perhaps there might be more of a handle to be had on how to
disambiguate your calls. If you don't really need that
template-template-parameter, then your class can just be this:

struct X {

// template< template<typena me> class TT, typename T >
template<typena me T >
static void f(const T& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}
// template< template<typena me> class TT >
static void f(const Test3& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f( c.t ); // <== error here
}

};
I am surprised. Why would the other template arg
cause the call to be ambiguos?


Because in my simplified version, that second version of f is not a
template.
-leor
-leor


Schobi


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
Leor Zolman <le**@bdsoft.co m> wrote:
[...]
Because in my simplified version, that second version of f is not a
template.
<ahem>
Right. Sorry for that stupid question.
-leor


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #5
Hendrik Schober wrote in news:c6******** **@news1.transm edia.de in
comp.lang.c++:
Hi,

I have a problem, that boils down to the following code:

#include <iostream>
#include <typeinfo>

class Test1 {};
class Test2 {};
class Test3 {};

template< typename T >
struct Cont { T t; };

template< typename T >
class TT { public: TT(T) {} };

struct X {

template< template<typena me> class TT, typename T >
static void f(const T& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

Here's a Hack-around:

template< template<typena me> class TT, typename T >
static void f(const volatile T& tv)
{
T const &t = const_cast< T const & >( tv );
std::cout << '\t' << typeid(t).name( ) << std::endl;
}
template< template<typena me> class TT >
static void f(const Test3& t)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f<TT>( c.t ); // <== error here
}

};

int main()
{
Cont<Test1> c1;
Cont<Test2> c2;
Cont<Test3> c3;

X::f< TT, Test1 >(c1);
X::f< TT, Test2 >(c2);
X::f< TT, Test3 >(c3);

return 0;
}

For this Comeau and CW9 report (in the line marked
"error here") that the call is ambigous. (VC7.1
accepts the code, BTW.)
Neither is a better match (they're both exact) and neither is more
specialized, and since there is no such thing as a function partial
specailization, we can't make it so.
First, I don't see what's wrong, but overloading
rules, especially in conjunctions with templates,
are too complicated for me to remember them anyway.
Second, I need a solution. I'm sure I would get it
to work if I would use a class template and partial
specialize it. However, the original code is long
and complex enough without yet another helper class
template...
I would hope that, once the problem is known, a
simpler way could be found. AFAIK, function template
overloading _is_ possible, after all.


I Think Comeau/CW (also g++ 3.2 & 3.4 pre-release) is correct here.
Though I wouldn't bet my shirt on it.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6
Rob Williscroft <rt*@freenet.co .uk> wrote:
[...]
Here's a Hack-around:

template< template<typena me> class TT, typename T >
static void f(const volatile T& tv)
{
T const &t = const_cast< T const & >( tv );
std::cout << '\t' << typeid(t).name( ) << std::endl;
}
Ugh.
[...]

Neither is a better match (they're both exact) and neither is more
specialized,
I always thought a types always is a
better match than a template arg?
Well, I guess I'm wrong...
and since there is no such thing as a function partial
specailization, we can't make it so.

I was afraid I'd have to create a helper
class template.
[...]
Rob.

Thanks,

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Jul 22 '05 #7
Hendrik Schober <Sp******@gmx.d e> wrote:
[...]


Thanks to everybody who cared. I found this:

template< template<typena me> class TT, typename T >
static void f(const T& t, CBool<false> /*isTest3*/)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const T& t, CBool<true> /*isTest3*/)
{
std::cout << '\t' << typeid(t).name( ) << std::endl;
}

template< template<typena me> class TT, typename T >
static void f(const Cont<T>& c)
{
f<TT>( c.t, CBool<CIsSameTy pe<T,Test3>::re sult>() );
}

Comeau accepts it, so I assume it's OK...

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
4704
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense...
4
6456
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a...
4
2389
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
2
2433
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include...
16
16216
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know...
2
1576
by: recover | last post by:
#include <stdio.h> template<class T> class TpHello { public: int GetHash(){return 0;} protected: private: T a;
1
2496
by: nyl2002 | last post by:
I have written the following very short template class in testclass.h: template<typename C,typename T> class TestClass { public: TestClass() {}; ~TestClass(); private: T testFunction(T dummy); };
15
1921
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " << 12.33 << " and a number";
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.