{ This article is multi-posted to [comp.lang.c++]. -mod }
I want to overload function to swap two others types, for example,
type<T>, T* and so on,
but I can't compile the following code at VC 6.0.
The compiler says :
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
---------------------------------------------------------------------------�----------------------------------
template < typename T >
void swap(T &lhs, T &rhs)
{
// do some thing
}
template < typename T >
void swap(T *lhs, T *rhs)
{
// do some thing
}
template < typename T >
struct type{};
template < typename T >
void swap(type<T> &lhs, type<T> &rhs)
{
// do some thing
}
int main()
{
int *p1,*p2;
type<int> t1, t2;
swap(t1, t2);
swap(p1, p2);
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ruki
|
6/30/2008 8:01:52 AM |
|
On Jun 30, 5:01 pm, Ruki <war...@gmail.com> wrote:
> I want to overload function to swap two others types, for example,
> type<T>, T* and so on,
> but I can't compile the following code at VC 6.0.
>
> The compiler says :
> error C2667: 'swap' : none of 2 overload have a best conversion
> error C2668: 'swap' : ambiguous call to overloaded function
> ---------------------------------------------------------------------------�----------------------------------
> template < typename T >
> void swap(T &lhs, T &rhs)
> {
> // do some thing
>
> }
>
> template < typename T >
> void swap(T *lhs, T *rhs)
> {
> // do some thing
>
> }
>
> template < typename T >
> struct type{};
>
> template < typename T >
> void swap(type<T> &lhs, type<T> &rhs)
> {
> // do some thing
>
> }
>
> int main()
> {
> int *p1,*p2;
> type<int> t1, t2;
> swap(t1, t2);
> swap(p1, p2);
>
Hello,
Please always include minimal compilable code.
Adding '#include <algorithm>' to the top of the above code, and
closing the 'main' block, the code compiles fine with both Microsoft
Visual C++ 2005 and Comeau C++ online compiler. You may want to try
updating your compiler.
Regards.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
alasham
|
7/1/2008 6:44:41 PM
|
|
On Jun 30, 8:01 pm, Ruki <war...@gmail.com> wrote:
> { This article is multi-posted to [comp.lang.c++]. -mod }
>
> I want to overload function to swap two others types, for example,
> type<T>, T* and so on,
> but I can't compile the following code at VC 6.0.
>
> The compiler says :
> error C2667: 'swap' : none of 2 overload have a best conversion
> error C2668: 'swap' : ambiguous call to overloaded function
> ---------------------------------------------------------------------------��----------------------------------
> template < typename T >
> void swap(T &lhs, T &rhs)
> {
> // do some thing
>
> }
>
> template < typename T >
> void swap(T *lhs, T *rhs)
> {
> // do some thing
>
> }
>
> template < typename T >
> struct type{};
>
> template < typename T >
> void swap(type<T> &lhs, type<T> &rhs)
> {
> // do some thing
>
> }
>
> int main()
> {
> int *p1,*p2;
> type<int> t1, t2;
> swap(t1, t2);
> swap(p1, p2);
>
> --
What you have tried to achieve is something like partial template
specialization of functions - which C++ does not allow. You could
perhaps use a function object with different overloads of the function-
call operator. Something like:
template < typename T >
struct type{};
template < typename T >
struct my_swap
{
void operator () (T &lhs, T &rhs) {
// do some thing
cout << "Regular overload" << endl;
}
void operator () (T *lhs, T *rhs) {
// do some thing
cout << "Pointer overload" << endl;
}
void operator () (type<T> &lhs, type<T> &rhs) {
cout << "type<...> overload" << endl;
}
};
int main()
{
int i1 = 4, i2 = 6;
int *p1 = &i1, *p2 = &i2;
type<int> t1, t2;
my_swap<int>()(i1, i2);
my_swap<int>()(t1, t2);
my_swap<int>()(p1, p2);
}
- Arindam
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Arindam
|
7/1/2008 6:52:27 PM
|
|
Arindam <arindam.muker...@gmail.com> wrote:
>
> template < typename T >
> struct type{};
>
> template < typename T >
> struct my_swap
> {
> void operator () (T &lhs, T &rhs) {
> // do some thing
> cout << "Regular overload" << endl;
> }
>
> void operator () (T *lhs, T *rhs) {
> // do some thing
> cout << "Pointer overload" << endl;
> }
>
> void operator () (type<T> &lhs, type<T> &rhs) {
> cout << "type<...> overload" << endl;
> }
>
> };
Thanks, It's a good idea. I slightly modify this code.
Like this:
template < typename T >
struct type{};
template < typename T >
struct swap_impl
{
static void swap(T &lhs, T &rhs)
{
// do some thing
}
static void swap(type<T> &lhs, type<T> &rhs)
{
// do some thing
}
};
template < typename T >
void swap(T &lhs, T &rhs)
{
swap_impl<T>::swap(lhs, rhs);
}
int main()
{
type<int> t1, t2;
swap(t1, t2);
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ruki
|
7/5/2008 3:58:10 AM
|
|
|
3 Replies
433 Views
(page loaded in 0.08 seconds)
Similiar Articles: 'Deferred' functions? - comp.lang.javascript> > I thought that if I am in the middle of a focus event listener, and I > call a function which sets focus on another element, javascript might > be left in a state where ... Non-member operator overloading, linker complains - comp.lang.c++ ...... referenced in function _main The problem seems to be the need to access private members in the overloaded ... and can only call (static or) const member functions ... VHDL-2002 vs VHDL-93 vs VHDL-87? - comp.lang.vhdl... VARIABLE may be; and Rising_edge has been overloaded to ... Ambiguous homographs are now legal due to generic ... globally static attribute or is an object or function call ... Avoiding problems with short-circuit primitive operator&& versus ...Specifically, it does not conform to the function call rule that all arguments ... almost impossible to track every expression's type and know if it calls an overloaded ... Bad use of stringstream temporary? - comp.lang.c++Unnamed temporary it is, but the call to the first member (to output the ... 0x), and runs as expected in that the rvalue-reference version of the overloaded function ... comp.soft-sys.matlab - page 42HELP: Calling Java 2 5 (10/21/2003 9:42:31 PM) Hello, I need to call some Java ... 2003 9:14:01 AM) How can you use subscripted reference within a member function? I overloaded ... std::map< MyString, MyString > comparison operator? - comp.lang ...... where the problem likely lies, namely the copy constructor and overloaded ... const member function, cannot call GetSize ... being a const member function, cannot call ... Error while evaluating TimerFCN for timer 'timer-1' - comp.soft ...... the case of stock % symbols that may be ambiguous ... most likely that the problem is in the stop() call ... function CQGAPI_StartUp() global m_CQGAPI ... Initialization of reference to non-const - comp.lang.c++.moderated ...Initialization of reference to non-const - comp.lang.c++.moderated ... You simply attempted to call a non-const member function through a const reference. ... with int ... time series correlation / autocorrelation / xcorr - comp.soft-sys ...Hi Matthias, you just need to call xcorr with the ... to calling the Signal Processing Toolbox xcorr function ... found the reference in question to be a little ambiguous ... error: ambiguous call to overloaded functionIt's the "pow(2,n)" part. It means that there are other versions of the function that take different arguments or different numbers of arguments, and you haven't ... Compiler Error C2668 (C++) - Microsoft Corporation: Software ...The specified overloaded function call could not be resolved. You may want to ... Error Message 'function' : ambiguous call to overloaded function 7/25/2012 10:20:19 AM
|