llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

invocation of next_permutation will not compile when using libc++

Open edfvogel opened this issue 5 years ago • 1 comments

Bugzilla Link 52219
Resolution INVALID
Resolved on Oct 19, 2021 09:27
Version 11.0
OS other
CC @JoeLoser,@mclow

Extended Description

The following program compiles with clang and the g++ headers, but will not compile using the libc++ headers. Visual Studio also cleanly compiles the program.

Perhaps the program is invalid, but other headers do allow it.

Here is a cut/paste from a cygwin session:

$ cat t.cpp

#include using namespace std; struct S { S(char ch = 0) : c(ch) {} S(const S& x) { } S& operator=(const S& x) { return (*this); } S& operator=(char ch) { return (*this); } bool operator<(const S& x) { return (c == x.c); } char c; };

int main(int, char *[]) { S a[10]; next_permutation(a, a + 1); }

VogelEd@XLB3502Q4E ~ $ clang++ t.cpp

VogelEd@XLB3502Q4E ~ $ clang++ -stdlib=libc++ t.cpp In file included from t.cpp:3: /usr/include/c++/v1/algorithm:716:71: error: invalid operands to binary expression ('const S' and 'const S') bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;} ~~~ ^ ~~~ /usr/include/c++/v1/algorithm:5612:13: note: in instantiation of member function 'std::__1::__less<S, S>::operator()' requested here if (__comp(*--__i, *__ip1))

Not important, just thought I'd post it.

Thanks again for your time,

Ed Vogel

edfvogel avatar Oct 19 '21 14:10 edfvogel

libc++ is correct I believe to fail to compile this program. The operator< needs to be marked const.

https://godbolt.org/z/cMGG4v51a is a working link showing that marking operator< as const successfully compiles the program.

JoeLoser avatar Oct 19 '21 16:10 JoeLoser