Was ist Erläuterung/Fix für den Fehler des Compiler beim Sortieren von Tupelstypen mit DeclType, Lambdas und Std :: ArraC++

Programme in C++. Entwicklerforum
Anonymous
 Was ist Erläuterung/Fix für den Fehler des Compiler beim Sortieren von Tupelstypen mit DeclType, Lambdas und Std :: Arra

Post by Anonymous »

Ich musste Typen in einem Tupel nach Ausrichtung sortieren, und als Ergebnis möchte ich nur einen neuen Tupel -Typ erhalten. < /p>
schrieb eine Implementierung. In einem Fall (automatisch Tuple) ) GCC und Clang geben jedoch in einem Fall (bei Verwendung von Conteval auto sort_tuple ein Fehler auf, und in einem anderen Fall (bei Verwendung der am meisten eingeschlossenen Funktion als Lambda) MSVC liefert jedoch einen Fehler, aber an unterschiedlichem (scheinbar) unterschiedlichem Grund. Code (Godbolt): < /p>
#include
#include
#include
#include
#include

using Index = std::size_t;
using Alignment = std::size_t;
struct Key {
Index old;
Alignment a;
};

template struct Elem {};
template struct Map : Elems... {};

template struct Tuple {};
struct alignas(2) s_1 {};
struct alignas(2) s_2 {};
struct alignas(2) s_3 {};
struct alignas(2) s_4 {};
using T1 = Tuple;

#define CLANG_GCC_DONT_WORK
//#define MSVC_DOESNT_WORK_BUT_DIFFERENT_PLACE

#if defined(CLANG_GCC_DONT_WORK)

consteval auto sort_tuple(auto tuple)
{

#elif defined(MSVC_DOESNT_WORK_BUT_DIFFERENT_PLACE)

using Sorted = decltype([]() consteval {
auto tuple = T1{};

#endif

constexpr auto size = [](::Tuple&){
return sizeof...(Ts);
}(tuple);

constexpr auto unsorted = []<
typename ...Ts,
auto ...Is
>(::Tuple&, std::index_sequence&&){
return std::array{::Key{Is, alignof(Ts)}...};
}(tuple, std::make_index_sequence{});

using Unsorted = decltype([](
::Tuple&, std::index_sequence&&
){
return ::Map<
::Elem...
>{};
}(tuple, std::make_index_sequence{}));

constexpr auto sorted = [](auto unsorted){
std::sort(
unsorted.begin(), unsorted.end(),
[](const auto& lhs, const auto& rhs){
return lhs.a > rhs.a;
}
);
return unsorted;
}(unsorted);

// gcc and clang complaining here
// (when using CLANG_GCC_DONT_WORK macro),
// I'm using trick with template argument deduction,
// and those, it seems, can't deduce
//
// also "sorted" std::array passed as template parameter,
// but when passing it through capture
// all compilers give error at this place
using Sorted = decltype([](
std::index_sequence&&
){

return ::Tuple<
decltype([](
[[maybe_unused]] ::Elem&& deduced_by_compiler
){
return T{};
}.template operator()(Unsorted{}))...
>{};
}.template operator()(std::make_index_sequence{}));

return Sorted{};

#if defined(CLANG_GCC_DONT_WORK)

};
using Sorted = decltype(sort_tuple(T1{}));

#elif defined(MSVC_DOESNT_WORK_BUT_DIFFERENT_PLACE)

}());

#endif

int main() {}
< /code>
Also: < /p>

Ist es ein Fehler der Compiler und alles nach Standard? Eine funktionierende Implementierung einer solchen Sortierung in C ++ 20 und unten würde ich mich freuen, wenn Sie sie in Kommentaren oder etwas

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post