[問題] template的語法

看板C_and_CPP作者 (raison detre)時間11年前 (2014/03/05 10:32), 編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/4 (看更多)
請問一下各位 以下是我class中的一個template method 只要傳進 vector, deque或是list就可以運作 但我希望可以限制使用者傳的是 std::vector<FileSystem::Info> std::deque<FileSystem::Info> std::list<FileSystem::Info> 之類的 避免使用者傳了 std::vector<std::string> std::deque<std::string> std::list<std::string> 之類的東西 請問我語法該如何修改 template<typename T> void getFiles(T& t) { DIR* _dir = opendir(m_name.c_str()); if(_dir==NULL) return; while(true) { dirent* _dirent = readdir(_dir); if ((_dirent == NULL) || (_dirent->d_name == NULL)) break; if(strcmp(_dirent->d_name,"..")==0 || strcmp(_dirent->d_name,".")==0 || strcmp(_dirent->d_name,"/")==0 ) continue; FileSystem::Info _info = this->join(std::string(_dirent->d_name)); t.push_back(_info); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.250.21.20

03/05 22:01, , 1F

03/05 22:25, , 2F
簡潔漂亮,c++11以後的作法?
03/05 22:25, 2F

03/05 22:43, , 3F
c++11 的沒錯
03/05 22:43, 3F

03/06 01:43, , 4F
是因為呼叫了某些成員函式所以才作這樣限制?
03/06 01:43, 4F
文章代碼(AID): #1J5lrI3p (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1J5lrI3p (C_and_CPP)