使用C ++ 14,我试图扩展this实现以支持将在外部调用的ObserverCallback方法。
MyClass容器使用元组向量,其类型由可变参数模板指定。然后可以使用访问<来访问向量。 T>()。
我需要的是实现一个应该向量添加元素的观察者。由于元素的类型可以是元组中的任何类型,我也将其模板化:
template <typename ...T>
class Wrapper{
public:
MyClass<T...> mc;
template <typename U>
void ObserverCallback(const U& element){
mc.access<U>().push_back(element);
}
};
Wrapper类似乎有错误:
variadic2.cpp: In member function 'void Wrapper<T>::ObserverCallback(const U&)':
variadic2.cpp:71:20: error: expected primary-expression before '>' token
mc.access<U>().push_back(element);
^
variadic2.cpp:71:22: error: expected primary-expression before ')' token
mc.access<U>().push_back(element);
^
variadic2.cpp: In function 'int main(int, char**)':
variadic2.cpp:87:10: error: 'class MyClass<float, std::reference_wrapper<int>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >' has no member named 'ObserverCallback'
w.mc.ObserverCallback(string("a"));
作为参考,我还附上了完整的源列表:
#include <tuple>
#include <vector>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
template <class T1, class T2>
struct SameType
{
static const bool value = false;
};
template<class T>
struct SameType<T, T>
{
static const bool value = true;
};
template <typename... Types>
class MyClass
{
public:
typedef tuple<vector<Types>...> vtype;
vtype vectors;
template<int N, typename T>
struct VectorOfType: SameType<T,
typename tuple_element<N, vtype>::type::value_type>
{ };
template <int N, class T, class Tuple,
bool Match = false> // this =false is only for clarity
struct MatchingField
{
static vector<T>& get(Tuple& tp)
{
// The "non-matching" version
return MatchingField<N+1, T, Tuple,
VectorOfType<N+1, T>::value>::get(tp);
}
};
template <int N, class T, class Tuple>
struct MatchingField<N, T, Tuple, true>
{
static vector<T>& get(Tuple& tp)
{
return std::get<N>(tp);
}
};
template <typename T>
vector<T>& access()
{
return MatchingField<0, T, vtype,
VectorOfType<0, T>::value>::get(vectors);
}
};
template <typename ...T>
class Wrapper{
public:
MyClass<T...> mc;
template <typename U>
void ObserverCallback(const U& element){
mc.access<U>().push_back(element);
}
};
int main( int argc, char** argv )
{
int twelf = 12.5;
typedef reference_wrapper<int> rint;
Wrapper<float, rint, string> w;
vector<rint>& i = w.mc.access<rint>();
i.push_back(twelf);
w.mc.access<float>().push_back(10.5);
w.ObserverCallback(string("a"));
cout << "Test:\n";
cout << "floats: " << w.mc.access<float>()[0] << endl;
cout << "ints: " << w.mc.access<rint>()[0] << endl;
cout << "strings: " << w.mc.access<string>()[0] << endl;
//w.access<double>();
return 0;
}
答案 0 :(得分:5)
mc
的类型取决于T...
,因此您必须指定其access
成员应该是一个功能模板:
mc.template access<U>().push_back(element);
// ^^^^^^^^