this (un)problemを解決しようとしている間に、bool
が偽であり、その値が!
(値ではない)も偽であるということを、間に合わせて非常に奇妙な動作に気付きました。私はこれがどのように可能であるか知りたいです。この原因コードは次のとおりです。ブールとそれ以外の値はどちらもfalseになりますか?
template<typename T, typename TID = unsigned int>
struct AId {
typedef AId<T, TID> type;
typedef T handled_type;
typedef TID value_type;
private:
value_type id;
template<typename _T> struct IsIncrementable
{
template<typename _U> using rm_ref = typename std::remove_reference<_U>::type;
typedef char (&yes)[1];
typedef char (&no)[2];
template<class _U>
static yes test(_U *data, typename std::enable_if<
std::is_same<_U, rm_ref<decltype(++(*data))>>::value
>::type * = 0);
static no test(...);
static const bool value = sizeof(yes) == sizeof(test((rm_ref<_T> *)0));
};
public:
explicit AId(const value_type &id) : id(id) {}
...
//IsIncrementable<value_type>::value is false:
//error: no type named 'type' in 'struct std::enable_if<false, int>'
template<typename std::enable_if<IsIncrementable<value_type>::value, int>::type = 0>
type operator++(int /*postfix*/) { type old(id); ++id; return old; }
//!IsIncrementable<value_type>::value is also false:
//error: no type named 'type' in 'struct std::enable_if<false, int>'
template<typename std::enable_if<!IsIncrementable<value_type>::value, int>::type = 0>
type operator++(int /*postfix*/) { type old(id); ++id; return old; }
};
それはどのようにIsIncrementable<value_type>::value
がfalseで!IsIncrementable<value_type>::value
も虚偽である可能性がありますか?
コード内のコメントを参照してください。エラーは両方ともenable_ifです。したがって、enable_ifの両方の引数はfalseです。 –
Olivetree
これを見ると、バリーの答えはあなたが[ここ](https://stackoverflow.com/questions/39998793/sfinae-to-have-a-class-member-only-if-possible)で参照した問題を解決し、私よりも簡単な答えです。 –
@Guillaume Racicotので、他のSFINAEの使用にも誤認はありませんか? (あまり面白い事実ではない:私はすでに前に似たようなコードを使用していたが、別のアプローチに賛成でそれを落として、もはやそれを持っていなかった) – Olivetree