public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111504] New: compare operator not defined for recursive data types on C++20
@ 2023-09-20 20:36 xgao at nvidia dot com
2023-09-20 21:01 ` [Bug c++/111504] " pinskia at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: xgao at nvidia dot com @ 2023-09-20 20:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111504
Bug ID: 111504
Summary: compare operator not defined for recursive data types
on C++20
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: xgao at nvidia dot com
Target Milestone: ---
Related bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111316
The following code works on C++17 but not C++20:
#include <cstdint>
#include <type_traits>
#include <vector>
template <typename T1, typename T2>
static auto hasLessThanHelper(int)
-> decltype(std::declval<T1>() < std::declval<T2>(), std::true_type{});
template <typename, typename>
static auto hasLessThanHelper(long) -> std::false_type;
template <typename T1, typename T2>
struct hasLessThan : decltype(hasLessThanHelper<T1, T2>(0)) {};
struct DynamicType {
using T1 = int64_t;
using T2 = std::vector<DynamicType>;
};
template <
typename DT,
typename = std::enable_if_t<
(hasLessThan<typename DT::T1, typename DT::T1>::value ||
hasLessThan<typename DT::T1, typename DT::T2>::value ||
hasLessThan<typename DT::T2, typename DT::T1>::value ||
hasLessThan<typename DT::T2, typename DT::T2>::value)>>
inline constexpr bool operator<(const DT& x, const DT& y) {
// implementation omitted
return true;
}
int main() {
using DT = DynamicType;
// This assert works on C++17, but fails on C++20
static_assert(hasLessThan<std::vector<DT>, std::vector<DT>>::value);
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/111504] compare operator not defined for recursive data types on C++20
2023-09-20 20:36 [Bug c++/111504] New: compare operator not defined for recursive data types on C++20 xgao at nvidia dot com
@ 2023-09-20 21:01 ` pinskia at gcc dot gnu.org
2023-09-20 21:23 ` xgao at nvidia dot com
2023-09-22 15:50 ` xgao at nvidia dot com
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-20 21:01 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111504
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fails for the same reason with clang (both with libstdc++ and libc++) ....
Are you sure this is valid C++ 20 code?
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/111504] compare operator not defined for recursive data types on C++20
2023-09-20 20:36 [Bug c++/111504] New: compare operator not defined for recursive data types on C++20 xgao at nvidia dot com
2023-09-20 21:01 ` [Bug c++/111504] " pinskia at gcc dot gnu.org
@ 2023-09-20 21:23 ` xgao at nvidia dot com
2023-09-22 15:50 ` xgao at nvidia dot com
2 siblings, 0 replies; 4+ messages in thread
From: xgao at nvidia dot com @ 2023-09-20 21:23 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111504
--- Comment #2 from Xiang Gao <xgao at nvidia dot com> ---
(In reply to Andrew Pinski from comment #1)
> Fails for the same reason with clang (both with libstdc++ and libc++) ....
>
> Are you sure this is valid C++ 20 code?
I am not 100% sure, but my understanding is, for the SFINAE in
inline constexpr bool operator<(const DT& x, const DT& y)
The first condition
hasLessThan<typename DT::T1, typename DT::T1>::value
is just hasLessThan<int64_t, int64_t>::value, which should be true, and (true
|| anything) is always true.
So the condition in SFINAE should be easily evaluated as true. So the operator<
for DynamicType should be defined.
And if operator< is defined, then the operator<=> of std::vector<DynamicType>
should also be defined. This can be validated by changing the definition of
operator< by only keeping the first condition:
template <
typename DT,
typename = std::enable_if_t<
(hasLessThan<typename DT::T1, typename DT::T1>::value)>>
inline constexpr bool operator<(const DT& x, const DT& y) {
// implementation omitted
return true;
}
and then both g++ and clang++ will pass.
I do observe the same error on clang, but this https://cpp.sh/ seems to compile
without problem on C++20.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/111504] compare operator not defined for recursive data types on C++20
2023-09-20 20:36 [Bug c++/111504] New: compare operator not defined for recursive data types on C++20 xgao at nvidia dot com
2023-09-20 21:01 ` [Bug c++/111504] " pinskia at gcc dot gnu.org
2023-09-20 21:23 ` xgao at nvidia dot com
@ 2023-09-22 15:50 ` xgao at nvidia dot com
2 siblings, 0 replies; 4+ messages in thread
From: xgao at nvidia dot com @ 2023-09-22 15:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111504
--- Comment #3 from Xiang Gao <xgao at nvidia dot com> ---
Cross posted at: https://github.com/llvm/llvm-project/issues/67056
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-09-22 15:50 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-20 20:36 [Bug c++/111504] New: compare operator not defined for recursive data types on C++20 xgao at nvidia dot com
2023-09-20 21:01 ` [Bug c++/111504] " pinskia at gcc dot gnu.org
2023-09-20 21:23 ` xgao at nvidia dot com
2023-09-22 15:50 ` xgao at nvidia dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).