vendredi 25 août 2023

std::swap says could not find matching operands on Mac however it works on compiler explorer [closed]

I was writing a swap function for my class. It looks like this

class foo:public base
{
    void swap(foo& lhs, foo& rhs) {
        base& lhs_b = static_cast<base&>(lhs);
        base& rhs_b = static_cast<base&>(rhs);
        std::swap(lhs_b,rhs_b);
        std::swap(lhs.a,rhs.a);
    }
}

The above swap works and I don't see a problem with it. What I was a little curious was that before doing that I mistyped the above and it looked like this

void swap(foo& lhs, foo& rhs) {
        foo& lhs_b = static_cast<foo&>(lhs);
        foo& rhs_b = static_cast<foo&>(rhs);
        std::swap(lhs_b,rhs_b); <---Error here - No matching function call for swap
        std::swap(lhs.a,rhs.a);
    }

My question is why did swap tell me that there is no matching function call for swap for those parameters . Why did swap not work for foo& types but works for base& types ?

Update:

Here is a minimal reproducible example. Seems like the swap does not like the copy constructor. I would be interested in knowing why and how to make my type work with std::swap

#include <iostream>

class base {

};

class foo : public base {
public:
    foo() {
        std::cout << "Regular constructor \n" ;
    }

    //The swap ensures bases are swapped as well
    void swap(foo& lhs, foo& rhs) {
        foo& lhs_b = static_cast<foo&>(lhs);
        foo& rhs_b = static_cast<foo&>(rhs);
        std::swap(lhs_b,rhs_b);
        std::swap(lhs.a,rhs.a);
    }

    foo(const foo& f) : base(f) {
        this->a = f.a;
    }

private:
    int a;
};

int main()
{
    std::cout << "Hello";
}

Seems like it has an issue with the copy constructor of foo This is the error that I am getting

 cmake-build-debug /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -fcolor-diagnostics -std=gnu++17 -MD -MT CMakeFiles/test.dir/main.cpp.o -MF CMakeFiles/test.dir/main.cpp.o.d -v -o CMakeFiles/test.dir/main.cpp.o -c /Users/user12/CLionProjects/test/main.cpp
Apple clang version 14.0.3 (clang-1403.0.14.5)
Target: arm64-apple-darwin22.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple arm64-apple-macosx13.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fobjc-msgsend-selector-stubs -target-sdk-version=13.3 -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +crc -target-feature +lse -target-feature +rdm -target-feature +crypto -target-feature +dotprod -target-feature +fp-armv8 -target-feature +neon -target-feature +fp16fml -target-feature +ras -target-feature +rcpc -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sm4 -target-feature +sha3 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -fallow-half-arguments-and-returns -mllvm -treat-scalable-fixed-error-as-warning -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 850 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3 -dependency-file CMakeFiles/test.dir/main.cpp.o.d -skip-unused-modulemap-deps -MT CMakeFiles/test.dir/main.cpp.o -sys-header-deps -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -std=gnu++17 -fdeprecated-macro -fdebug-compilation-dir=/Users/user12/CLionProjects/test/cmake-build-debug -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -no-opaque-pointers -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -mllvm -disable-aligned-alloc-awareness=1 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o CMakeFiles/test.dir/main.cpp.o -x c++ /Users/user12/CLionProjects/test/main.cpp
clang -cc1 version 14.0.3 (clang-1403.0.14.5) default target arm64-apple-darwin22.4.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/System/Library/Frameworks (framework directory)
End of search list.
/Users/user12/CLionProjects/test/main.cpp:32:9: error: no matching function for call to 'swap'
        std::swap(lhs_b,rhs_b);
        ^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__utility/swap.h:33:85: note: candidate template ignored: requirement 'is_move_constructible<foo>::value' was not satisfied [with _Tp = foo]
inline _LIBCPP_INLINE_VISIBILITY __swap_result_t<_Tp> _LIBCPP_CONSTEXPR_AFTER_CXX17 swap(_Tp& __x, _Tp& __y)
                                                                                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__utility/pair.h:416:1: note: candidate template ignored: could not match 'pair<_T1, _T2>' against 'foo'
swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__utility/swap.h:42:1: note: candidate template ignored: could not match '_Tp[_Np]' against 'foo'
swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/tuple:251:6: note: candidate template ignored: could not match '__tuple_leaf<_Ip, _Hp, _Ep>' against 'foo'
void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/tuple:259:6: note: candidate template ignored: could not match '__tuple_leaf<_Ip, _Hp, _Ep>' against 'foo'
void swap(const __tuple_leaf<_Ip, _Hp, _Ep>& __x, const __tuple_leaf<_Ip, _Hp, _Ep>& __y)
     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/tuple:1334:1: note: candidate template ignored: could not match 'tuple<_Tp...>' against 'foo'
swap(tuple<_Tp...>& __t, tuple<_Tp...>& __u)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/compressed_pair.h:159:6: note: candidate template ignored: could not match '__compressed_pair<_T1, _T2>' against 'foo'
void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:553:1: note: candidate template ignored: could not match 'unique_ptr<_Tp, _Dp>' against 'foo'
swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/shared_ptr.h:1332:1: note: candidate template ignored: could not match 'shared_ptr<_Tp>' against 'foo'
swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/shared_ptr.h:1627:1: note: candidate template ignored: could not match 'weak_ptr<_Tp>' against 'foo'
swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/variant:1733:6: note: candidate template ignored: could not match 'variant<_Types...>' against 'foo'
auto swap(variant<_Types...>& __lhs, variant<_Types...>& __rhs)
     ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/array:442:1: note: candidate template ignored: could not match 'array<_Tp, _Size>' against 'foo'
swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__hash_table:2667:1: note: candidate template ignored: could not match '__hash_table<_Tp, _Hash, _Equal, _Alloc>' against 'foo'
swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/optional:1543:1: note: candidate template ignored: could not match 'optional<_Tp>' against 'foo'
swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/unordered_map:635:1: note: candidate template ignored: could not match '__unordered_map_hasher<_Key, _Cp, _Hash, _Pred, __b>' against 'foo'
swap(__unordered_map_hasher<_Key, _Cp, _Hash, _Pred, __b>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/unordered_map:748:1: note: candidate template ignored: could not match '__unordered_map_equal<_Key, _Cp, _Pred, _Hash, __b>' against 'foo'
swap(__unordered_map_equal<_Key, _Cp, _Pred, _Hash, __b>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/unordered_map:1870:1: note: candidate template ignored: could not match 'unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>' against 'foo'
swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/unordered_map:2576:1: note: candidate template ignored: could not match 'unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>' against 'foo'
swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__bit_reference:106:1: note: candidate template ignored: could not match '__bit_reference<_Cp>' against 'foo'
swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__bit_reference:116:1: note: candidate template ignored: could not match '__bit_reference<_Cp>' against 'foo'
swap(__bit_reference<_Cp> __x, __bit_reference<_Dp> __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__bit_reference:126:1: note: candidate template ignored: could not match '__bit_reference<_Cp>' against 'foo'
swap(__bit_reference<_Cp> __x, bool& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__bit_reference:136:1: note: candidate template ignored: could not match '__bit_reference<_Cp>' against 'foo'
swap(bool& __x, __bit_reference<_Cp> __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__split_buffer:639:1: note: candidate template ignored: could not match '__split_buffer<_Tp, _Allocator>' against 'foo'
swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/vector:3285:1: note: candidate template ignored: could not match 'vector<_Tp, _Allocator>' against 'foo'
swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/function.h:1250:1: note: candidate template ignored: could not match 'function<_Rp (_ArgTypes...)>' against 'foo'
swap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/string:4504:1: note: candidate template ignored: could not match 'basic_string<_CharT, _Traits, _Allocator>' against 'foo'
swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__mutex_base:262:1: note: candidate template ignored: could not match 'unique_lock<_Mutex>' against 'foo'
swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/list:2335:1: note: candidate template ignored: could not match 'list<_Tp, _Alloc>' against 'foo'
swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__tree:2734:1: note: candidate template ignored: could not match '__tree<_Tp, _Compare, _Allocator>' against 'foo'
swap(__tree<_Tp, _Compare, _Allocator>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/map:668:1: note: candidate template ignored: could not match '__map_value_compare<_Key, _CP, _Compare, __b>' against 'foo'
swap(__map_value_compare<_Key, _CP, _Compare, __b>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/map:1727:1: note: candidate template ignored: could not match 'map<_Key, _Tp, _Compare, _Allocator>' against 'foo'
swap(map<_Key, _Tp, _Compare, _Allocator>& __x,
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/map:2318:1: note: candidate template ignored: could not match 'multimap<_Key, _Tp, _Compare, _Allocator>' against 'foo'
swap(multimap<_Key, _Tp, _Compare, _Allocator>& __x,

However when I run it on compiler explorer I don't get that error

This is my build info enter image description here

Aucun commentaire:

Enregistrer un commentaire