[英]How to define member template function of variadic class template
我正在嘗試使用成員模板函數實現可變參數 class 模板,其模板 arguments 獨立於 class 模板參數,但我在定義成員模板時遇到了麻煩。
我將我的問題簡化為嘗試編譯它(抱歉無法弄清楚如何進一步簡化):
#include <iostream>
#include <string>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
template <class... Types>
class Foo {
public:
Foo();
template <class T>
T& at(const std::string& key);
template <class T>
void insert(const std::string& key, const T& value);
private:
std::tuple<std::unordered_map<std::string, Types>...> sets_;
std::unordered_map<std::type_index, size_t> type_to_pos_;
};
template<class... Types>
Foo<Types...>::Foo() {
std::vector<std::type_index> type_indices{std::type_index(typeid(Types))...};
for (size_t i = 0; i < type_indices.size(); i++) {
this->type_to_pos_.insert({type_indices[i], i});
}
}
template<class T, class... Types>
T& Foo<Types...>::at(const std::string& key) {
std::type_index type_idx{std::type_index(typeid(T))};
size_t pos;
pos = this->type_to_pos_.at(type_idx);
return std::get<pos>(this->sets_).at(key);
}
template <class T, class... Types>
void Foo<Types...>::insert(const std::string& key, const T& value) {
std::type_index type_idx{std::type_index(typeid(T))};
size_t pos;
pos = this->type_to_pos_.at(type_idx);
std::get<pos>(this->sets_).insert({key, value});
}
int main(int argc, char** argv) {
Foo<int, float, double> foo{};
foo.insert("key", 1.0f);
std::cout << foo.at<float>("key") << std::endl;
return 0;
}
嘗試編譯 (C++11) 時,出現以下錯誤:
$ make
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
/Users/Jasper/cpp_projects/playground/main.cpp:33:19: error: nested name specifier 'Foo<Types...>::'
for declaration does not refer into a class, class template or class template partial
specialization
T& Foo<Types...>::at(const std::string& key) {
~~~~~~~~~~~~~~~^
/Users/Jasper/cpp_projects/playground/main.cpp:37:9: error: invalid use of 'this' outside of a
non-static member function
pos = this->type_to_pos_.at(type_idx);
^
/Users/Jasper/cpp_projects/playground/main.cpp:38:24: error: invalid use of 'this' outside of a
non-static member function
return std::get<pos>(this->sets_).at(key);
^
/Users/Jasper/cpp_projects/playground/main.cpp:38:40: error: use of undeclared identifier 'key'
return std::get<pos>(this->sets_).at(key);
^
/Users/Jasper/cpp_projects/playground/main.cpp:42:21: error: nested name specifier 'Foo<Types...>::'
for declaration does not refer into a class, class template or class template partial
specialization
void Foo<Types...>::insert(const std::string& key, const T& value) {
~~~~~~~~~~~~~~~^
/Users/Jasper/cpp_projects/playground/main.cpp:43:19: error: redefinition of 'type_idx'
std::type_index type_idx{std::type_index(typeid(T))};
^
/Users/Jasper/cpp_projects/playground/main.cpp:34:19: note: previous definition is here
std::type_index type_idx{std::type_index(typeid(T))};
^
/Users/Jasper/cpp_projects/playground/main.cpp:44:10: error: redefinition of 'pos'
size_t pos;
^
/Users/Jasper/cpp_projects/playground/main.cpp:35:10: note: previous definition is here
size_t pos;
^
/Users/Jasper/cpp_projects/playground/main.cpp:46:9: error: invalid use of 'this' outside of a
non-static member function
pos = this->type_to_pos_.at(type_idx);
^
8 errors generated.
make[2]: *** [CMakeFiles/test.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
我很確定它歸結為第一個和第五個錯誤,但無法弄清楚我做錯了什么。 為什么Foo<Types...>
不引用 class 模板? 我怎樣才能解決這個問題?
編輯:添加了實用程序庫並修復了insert
的返回值。
PS 為了簡單起見,我刪除了所有異常檢查。
—————————- @songyuanyao 給出的答案解決了問題,但正如@songyuanyao 指出的那樣,在編譯時get
不知道pos
所以它不會編譯。 這個解決方案有助於解決這個問題。
您應該分開兩組模板參數:一組用於封閉的 class 模板,另一組用於成員 function 模板本身。 例如
template<class... Types> // for the enclosing class template
template<class T> // for the member template
T& Foo<Types...>::at(const std::string& key) {
...
}
template<class... Types> // for the enclosing class template
template<class T> // for the member template
void Foo<Types...>::insert(const std::string& key, const T& value) {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.