はthis質問に基づいて、私はis_vector
特性を実験:isVector関数がtrueを返さないのはなぜですか?
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
struct is_vector {
constexpr static bool value = false;
};
template<typename T>
struct is_vector<std::vector<T>> {
constexpr static bool value = true;
};
int main() {
int A;
vector<int> B;
cout << "A: " << is_vector<decltype(A)>::value << endl;
cout << "B: " << is_vector<decltype(B)>::value << endl;
return 0;
}
出力:
予想通り、この作品A: 0
B: 1
。しかし、私は、小さなヘルパー関数でis_vector
戻りB
ためfalse
これを置くしようとすると:
template<typename T>
constexpr bool isVector(const T& t) {
return is_vector<decltype(t)>::value;
}
...
cout << "B: " << isVector(B) << endl; // Expected ouptput: "B: 1"
出力:
B: 0
を私はここで何をしないのですか?
まあ、 'tは' const std :: vector& 'です。 –
また、迅速な対応のために@BoPerssonに感謝します。 –
ほぼ同じです:https://stackoverflow.com/questions/41996441/why-is-stdis-constvalue-false-even-though-ts-value-type-is-const –