Specialization STD :: Tuple_size und Std :: Get for std :: anwenden - KompilierungsfehlerC++

Programme in C++. Entwicklerforum
Anonymous
 Specialization STD :: Tuple_size und Std :: Get for std :: anwenden - Kompilierungsfehler

Post by Anonymous »

Ich versuche, std :: tuple_size und std :: Get für eine einfache Struktur spezialisiert zu werden, damit ich in einer generischen Funktion std :: anwenden damit verwenden kann. Wenn ich jedoch STD :: anwenden aufrufe, erhalte ich einen Kompilierungsfehler. Interessanterweise, wenn ich die Implementierung von std :: anwenden und std :: __ apply_impl in einen lokalen Namenspace kopiere und diese Version nennt, funktioniert alles einwandfrei.

Code: Select all

#include 
#include 
#include 
#include 

// Define a simple struct A with different types
struct A {
int i;
double d;
};

// Specialize std::tuple_size and std::get for A
namespace std {
template  struct tuple_size : std::integral_constant { };  // 3 members in A

template   constexpr decltype(auto) get (const A& a) { if constexpr (N == 0) return (a.i); else if constexpr (N == 1) return (a.d); }
}

// I copied below the code for std::__apply_impl, and std::apply
namespace My {
template 
constexpr decltype(auto) apply_impl(Callable&& _Obj, Tuple&& _Tpl,std::index_sequence)
{
using namespace std;
return std::invoke (forward(_Obj), get(forward(_Tpl))...);
}

template 
constexpr decltype(auto) apply(Callable&& Obj, Tuple&& Tpl)
{
using namespace std;
return apply_impl(forward(Obj), forward(Tpl), make_index_sequence{});
}
}

int main ()
{
A a {0, 1.5};
// #define COMPILER_ERROR // enabling the define breaks the compiler
#ifdef COMPILER_ERROR
std::apply ([](auto&&... args) {((std::cout

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post