私はこれを実行できるようにしたいと思います。特にテストがまたは寸法(今のところ)1または2のために反復可能ではないかもしれないかもしれないカスタムクラスを受け入れるようにテンプレート化されテンプレートを使用してファンクションメソッドが存在するかどうか再帰的にチェックしてください。
vector<int> p = {1, 2};
vector<vector<int>> q = {p, {0, 1}};
auto t = test(p);
auto u = test(q); // Fails with below implementation
。私は何が与えられたものがsize
機能を持っているかどうかをチェックすることによって何をすべきかを決定しようとしています。
template<typename T> struct hasSize {
template<typename U, size_t(U::*)() const> struct SFINAE {};
template<typename U> static char Test(SFINAE<U, &U::size>*);
template<typename U> static int Test(...);
static const bool value = sizeof(Test<T>(0)) == sizeof(char);
};
template<typename iterable> int test(const iterable &x, std::false_type) {
return (int) x;
}
template<typename iterable> int test(const iterable &x, std:: true_type) {
int total = 0;
for(auto &each : x)
total += test(each,
std::integral_constant<bool, hasSize<decltype(each)>::value>());
return total;
}
template<typename iterable> int test(const iterable &view) {
return test(view, std::true_type());
}
ことがメンバ変数ではなく、機能にのみ適用されるように見えたので、私は、this答えにあきらめた後here与えられた回答にhasSizeをベース。私もhas_const_reference_op
の修正版を最初の議論で試しましたが、これは同じ問題を抱えています。
このエラーは、SNIFAEが2回適用されないことを示しています。
error C2440: 'type cast':
cannot convert from 'const std::vector<int, std::allocator<_Ty>>' to 'int'
note: No user-defined-conversion operator available that can perform this conversion,
or the operator cannot be called
note: see reference to function template instantiation
'int test<iterable>(const iterable &, std::false_type)' being compiled
with iterable = std::vector<int,std::allocator<int>>
しかし、私には理由が分かりません。それはsize
メンバ関数のためのSFINAEチェックに失敗した参照型を生み出す -
おそらく 'hasSize :: type> :: value>()' –
のほうがいいかもしれませんが、それ以上のことをする必要があると思います型がネストされたコンテナで構成されているかどうかを判断する 'size()'関数の存在をチェックしてください。たとえば、文字列クラスには 'size()'関数があります。 –