を構築することが可能です私はタイプがvector<...>
であればリターンはvector<...>::size_type
になり、ない場合、それはint
なり、タイプを決定するためにstd::conditional
を使用するとします。 (ほんの一例)。はlazy_conditionalメタ関数
std::conditional
を使用するには、素朴な方法:
template<class V> struct is_vector : std::false_type{};
template<class T> struct is_vector<std::vector<T>> : std::true_type{};
template<class C>
using my_size_type = typename std::conditional<
not is_vector<C>::value,
int,
C::size_type // note that this line only makes sense when condition is false
>::type;
しかしこれが失敗C
がdouble
を言うのであればあるため、double::size_type
はそれが第二falseオプションの評価であったとしても、エラーになります。
したがって、false(または第2のfalse)文が評価されないような種類のlazy_conditional
があるかどうかは疑問です。
私はここに何かを見つけました:https://stackoverflow.com/a/5317659/225186しかし私は私の例を使用する方法を知らない。私はstd::conditional
を使用せずに同じ結果を取得する方法を知っている
注:何とか短絡するstd::conditional
をカプセル化lazy_conditional
がある場合
template<class V> struct my_size_type{typedef int type;};
template<class T> struct my_size_type<std::vector<T>>{typedef std::vector<T>::size_type type;};
質問です。私はhttps://stackoverflow.com/a/5317659/225186でアイデアを使用して、次のようこれに取得するために管理し、いくつかのトライアル・エラーの後
。また、C::size_type
は先験的な表現では全く出現しないので、std::lazy_conditional
を書くことはできないと考えるので、2段階式が必要です。
template<class C, bool B> struct false_case{
typedef void type;
};
template<class C> struct false_case<C, false>{
typedef typename C::size_type type;
};
template<class C>
using size_type = typename std::conditional<
not is_vector<C>::value,
int,
typename false_case<C, not is_vector<C>::value>::type
>::type;
それぞれのケースが異なるため、これをマクロに凝縮さえできませんでした。
[OK]を、私はそれのようにカプセル化することができないと思います。
あなたが本当にやりたいことは、「それ以外の場合は、存在する場合'です。とにかく、これはまた、フリー関数がメンバ関数よりも優れていることを示しています(フリー関数がジェネリックコードのメンバ関数より優れているように、T :: size_typeに頼るのではなく、std :: size_type_of :: typeを持っています)。 –
alfC
C::size_type
、int
」の場合は、その後、std::experimental::detected_or_t
はあなたの友達です'lazy_conditionalああ、これは私が何度も何度も発見したものです。最後に私がここでやったのと同じです:http://stackoverflow.com/questions/5839357/detect-operator-support-with-decltype-sfinae/18603716# 18603716 – alfC
'detected_or_t'互換性について:http://stackoverflow.com/questions/36418570/what-compiler-option-library-do-i-need-to-use-detect-or-t-type-trait?noredirect= 1#comment60486585_36418570 – alfC