2017-02-13 11 views
3

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 

を私はここで何をしないのですか?

+3

まあ、 'tは' const std :: vector & 'です。 –

+0

また、迅速な対応のために@BoPerssonに感謝します。 –

+0

ほぼ同じです:https://stackoverflow.com/questions/41996441/why-is-stdis-constvalue-false-even-though-ts-value-type-is-const –

答えて

6

ここでの問題はtconst std::vector<int>&で、struct is_vector<std::vector<T>>と一致しません。関数内で本当に欲しいのは、Tを使用することです。これは、動作するstd::vector<T>と推定されます。あなたはそれを得ること

template<typename T> 
constexpr bool isVector(const T& t) { 
     return is_vector<T>::value; 
} 
+0

説明をありがとう、今私は愚かな気がします。私はあなたの答えをできるだけ早く受け入れます。 –

+0

@PhilippLudwig問題ありません。喜んで助けてください。 – NathanOliver

関連する問題