Spaß haben mit boost :: hana. Ich möchte für eine bestimmte verschachtelte Art überprüfen, die in einem anderen Typ, wie ein Tag wirkt, so dass ich ausleihen von hana :: when_valid Beispiel und definiert eine Klasse is_S zusammen mit seiner SFINAE-fähigen Spezialisierung:Suche nach einem bestimmten verschachtelten Typ/Tag mit Boost hana
#include <iostream>
#include <boost/hana/core/when.hpp>
namespace hana = boost::hana;
#define V(x) std::cout << x << std::endl
struct S_tag { };
struct S {
using tag = S_tag;
};
struct T {
using tag = int;
};
template< typename T, typename = hana::when<true> >
struct is_S {
static constexpr bool value = false;
};
template< typename T >
struct is_S< T, hana::when_valid< typename T::tag > > {
static constexpr bool value = std::is_same<
typename T::tag, S_tag >::value;
};
int main() {
std::cout << "is_S ( S { }) = "; V ((is_S<S>::value));
std::cout << "is_S ( T { }) = "; V ((is_S<T>::value));
std::cout << "is_S (float { }) = "; V ((is_S<float>::value));
return 0;
}
Diese Drucke:
$ clang++ -std=c++1z sfinae.cpp && ./a.out | c++filt
is_S ( S { }) = 1
is_S ( T { }) = 0
is_S (float { }) = 0
gibt es eine einfachere/kürzere/prägnante Weise die gleiche Prüfung des Schreiben, mit dem Wert-Typ Berechnung von hana Philosophie in Einklang?
template<class...T>
constexpr std::integral_constant<bool,false> is_S(T const&...){ return {}; }
template<class T>
constexpr
std::integral_constant<bool,std::is_same<typename T::tag,S_tag>{}>
is_S(T const&){ return {}; }
Ich liebe wirklich die Kürze. Die Doktoren sind gut geschrieben und immens hilfreich, aber es dauert eine Weile, um tatsächlich das Gefühl für die Hana-Idiome zu bekommen. – Engineerist