cpp
1#include <iostream> 2#include <vector> 3#include <array> 4#include <string> 5#include <type_traits> 6 7using namespace std; 8 9template < typename T > 10struct is_vector : std::false_type {}; 11 12template < typename T > 13struct is_vector<std::vector<T>> : std::true_type {}; 14 15template < typename T > 16constexpr bool is_vector_v = is_vector<T>::value; 17 18template<typename T> 19typename std::enable_if<is_vector<T>::value>::type outVector(T vec) 20{ 21 for (const auto& v : vec) 22 { 23 cout << v << endl; 24 } 25} 26 27 28int main(void) { 29 std::vector<std::string> vec{ "1", "2", "3" }; 30 31 outVector(vec); 32 33}
C++メタ関数まとめよりvectorのみ実体化する関数を作成しましたが、
cpp
1template < typename T > 2constexpr bool is_vector_v = is_vector<T>::value;
この部分はなぜ必要なのでしょうか?
なくても問題なく動作はしました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。