-3

I have been trying to use the std::swap function in C++:

#include <iostream>

int main(int argc, char * argv[]){
    int i(2);
    int j(3);
    std::cout << i << std::endl << j << std::endl;
    using std::swap;
    swap(i,j);
    std::cout << i << std::endl << j << std::endl;
}

But I can't figure out why I have this error message in the editor under the swap function:

Invalid arguments '
Candidates are:
std::enable_if<74 0 value 14 std::__and_ 3 14 std::__not_ 1 14 std::__is_tuple_like 1 #0 14 std::is_move_constructible 1 #0 14 std::is_move_assignable 1 #074 0 value 14 std::__and_ 3 14 std::__not_ 1 14 std::__is_tuple_like 1 #0 14 std::is_move_constructible 1 #0 14 std::is_move_assignable 1 #0,void>::type swap(#0 &, #0 &)
std::enable_if<74 0 value 14 std::__and_ 2 14 std::__is_swappable 1 #0 14 std::__is_swappable 1 #174 0 value 14 std::__and_ 2 14 std::__is_swappable 1 #0 14 std::__is_swappable 1 #1,void>::type swap(std::pair<#0,#1> &, std::pair<#0,#1> &)
std::enable_if<bool15 7 74 0 value 14 std::__and_ 2 14 std::__is_swappable 1 #0 14 std::__is_swappable 1 #1 0,void>::type swap(std::pair<#0,#1> &, std::pair<#0,#1> &)
void swap(std::__cxx11::basic_string<#0,#1,#2> &, std::__cxx11::basic_string<#0,#1,#2> &)
std::enable_if<74 0 value 14 std::__is_swappable 1 #074 0 value 14 std::__is_swappable 1 #0,void>::type swap(#0 (&)[3 #1 0], #0 (&)[3 #1 0])
'

What is particularly strange is that it appears only in the editor but when I execute, it seems to work perfectly.

I've been trying to restart multiple times, and I also tried with different types of objects but nothing changed.

I am using Eclipse on Windows with MinGW Toolchain.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
F.Ferlande
  • 13
  • 2
  • Your code [compiles just fine on g++](http://coliru.stacked-crooked.com/a/ab8a2b791fd34e72) as C++17. What C++ version are you compiling against? – alter_igel Oct 17 '18 at 17:28
  • 11
    Try including the appropriate header: https://en.cppreference.com/w/cpp/algorithm/swap . (it's `#include `) – Justin Oct 17 '18 at 17:28
  • hitting "g++ -- version" gives me 8.1.0 as a version – F.Ferlande Oct 18 '18 at 07:05

1 Answers1

2

std::swap is defined in the header <utility>. Include the header first, and this will succeed.

The overloads being displayed in the error appear to refer to other std::swap implementations, which may be visible by the inclusion of <iostream>. For example, std::swap for std::__cxx11::basic_string (std::string) is defined in <string> -- and your implementation of <iostream> likely includes string (or at least forward-declares relevant parts) as part of its implementation, which makes the swap overload visible.

Human-Compiler
  • 11,022
  • 1
  • 32
  • 59
  • I tried with #include but it doesn't seem to change anything. Actually, I tried on Linux and it works just fine, so could it be a configuration problem with my IDE ? Basically I just downloaded Eclipse IDE for C/C++ Developers, and then some distribution of MinGW (and didn't forgot to add C:\MinGW\bin to the path) – F.Ferlande Oct 18 '18 at 07:09
  • It could be an issue with the configuration (e.g. eclipse using incorrect include paths or something), or even just an issue with bad indexing that needs to be updated. The index in Eclipse can sometimes get stale -- which requires rebuilding the index before errors go away. – Human-Compiler Oct 18 '18 at 12:25
  • The error message you are getting _may still be legitimate_ for a different standard library and/or version... Would you happen to know what the `__cplusplus` symbol is evaluating to in Eclipse's preprocessor? It can sometimes be incorrectly set to `199711L` (C++98), when it should be a more modern one. This will depend on the language settings of the project, but can be manually adjusted in Eclipse's settings. C++98 defined `swap` in ``, and C++11 moved it to `` -- so it's possible the IDE is preprocessing with a different C++ version than you are compiling with – Human-Compiler Oct 18 '18 at 12:34
  • If I'm not mistaken, the __cplusplus symbol is evaluated at 201402. But I agree, I think something was definitely wrong between the IDE preprocessor and the compiler. I tried to install another distribution of MinGW and it worked perfectly fine. Thanks a lot ! – F.Ferlande Oct 18 '18 at 15:06