Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

swap overload ambiguity

21 views
Skip to first unread message

wyn...@gmail.com

unread,
Jun 15, 2018, 4:19:36 AM6/15/18
to
I have a piece of test code to compare std::sort and my Wy::sort.

const int Test_Sort_Count=500;

template<typename T>
void test_sort_std(const int len)
{
assert(len>0);
using std::swap;
Wy::ErrNo r; // Wy::ErrNo is a class wraps type int
Wy::Array<T> arr(len,0); // Wy::Array is a dynamic array

// Random
for(int count=Test_Sort_Count; count>0; --count) {
for(int i=0; i<len; ++i) {
arr[i]=std::rand()%len;
}
std::sort(arr.begin(),arr.end()); // line 38
for(int i=1; i<len; ++i) {
if(arr[i]<arr[i-1]) {
WY_THROW( Wy::ErrNo() );
}
}
}
//.......
//.......
};

template<typename T> Wy::swap(T& a, T& b); is implemented in my library,
it used to compile fine. (probably caused by adding compile opt. -std=gnu++11)
Is there any one any idea how to solve this compilation problem?

-------------------
[]$make sp_sort

/usr/include/c++/8/bits/stl_algobase.h: In instantiation of ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = Wy::Timespec*; _ForwardIterator2 = Wy::Timespec*]’:
/usr/include/c++/8/bits/stl_algo.h:84:20: required from ‘void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = Wy::Timespec*; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/8/bits/stl_algo.h:1921:34: required from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = Wy::Timespec*; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/8/bits/stl_algo.h:1953:38: required from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = Wy::Timespec*; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/8/bits/stl_algo.h:1968:25: required from ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = Wy::Timespec*; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
/usr/include/c++/8/bits/stl_algo.h:4834:18: required from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = Wy::Timespec*]’
sp_sort.cpp:38:13: required from ‘void test_sort_std(int) [with T = Wy::Timespec]’
sp_sort.cpp:397:37: required from here
/usr/include/c++/8/bits/stl_algobase.h:148:11: error: call of overloaded ‘swap(Wy::Timespec&, Wy::Timespec&)’ is ambiguous
swap(*__a, *__b);
~~~~^~~~~~~~~~~~

--- Wy::Timespec is a class wrapper of ::timespec

Bo Persson

unread,
Jun 15, 2018, 4:35:20 AM6/15/18
to
Why do you have a swap functions with the same signature as the std::swap?

template<typename T> Wy::swap(T& a, T& b); says "for absolutely ANY
type, do this". Which is a bit wide for an interface.

You can avoid the ambiguity by only supplying the more specialized overload

swap(your_type&, your_type&);



Bo Persson



wyn...@gmail.com

unread,
Jun 15, 2018, 7:25:18 AM6/15/18
to
Bo Persson於 2018年6月15日星期五 UTC+8下午4時35分20秒寫道:
As regard to the 'swap'. It's because I adopted 'the move ctor' approach.
template<typename T> Wy::swap(T& a, T& b) is thus natuarly defined.
Even the standard C++ adopted rref latter, I decided not to use it.

> You can avoid the ambiguity by only supplying the more specialized overload
>
> swap(your_type&, your_type&);

This ambiguity issue occurs only in the present rewritting of the
library, all previous versions are fine. So I wonder where went wrong?
And how to fix it from the compiler error report.

>
>
>
> Bo Persson

0 new messages