[英]declaration of templated function (over container type) inside class and definition of it outside a template class over container type-
With reference to my problem here 关于我的问题在这里
I have a template class as follows: 我有一个模板类,如下所示:
//traits.hpp
namespace traits {
typedef std::vector <int> container_t;
typedef std::set <int> container_t2;
typedef std::list <int> container_t3;
};
//FOO.hpp
class FOO {
public:
static int a; static int b; static int c;
};
int FOO::a = 10; int FOO::b = 20; int FOO::c = 30;
// BAR.hpp
using namespace traits;
template <class FOO>
class BAR {
public:
BAR : m_a (FOO::a), m_b (FOO::b), m_c (FOO::c) { }
// I can happily do this. ===>> VALID
template <template <typename, typename> class ContainerT, typename ValueT>
void Initialize(ContainerT <ValueT, std::allocator <ValueT>>& container)
{
typedef ContainerT<ValueT, std::allocator <ValueT>> type;
int id = 0;
for (auto& i : container)
i = id++;
}
void DO_THIS ()
{
Initialize (my_container)
}
private:
container_t my_container;
int m_a, m_b, m_c;
}
// in main.
BAR <FOO> bar_object;
bar_object.DO_THIS (); // calls the initialize function. which is fine.
I just want to do this: define the Initialize template function outside the class. 我只想这样做:在类之外定义Initialize模板函数。
using namespace traits;
template <class FOO>
class BAR {
public:
BAR : m_a (FOO::a), m_b (FOO::b), m_c (FOO::c) { }
// prototype of Initialize
void Initialize ( **?? what to put here ??** );
// I cant put container_t& BECAUSE I have several containers such as
// set/vector/list.
// If I want to initialize the container using set / vector / list, the prototype
// will be incorrect.
// i cant declare a prototype for each container type.
void DO_THIS ()
{
Initialize (my_container1);
Initialize (my_container2);
}
private:
container_t1 my_container1;
container_t2 my_container2
int m_a, m_b, m_c;
};
// unable to define the function properly outside the class.
// gives template errors
template <template <typename, typename> class ContainerT, typename ValueT>
void Initialize(ContainerT <ValueT, std::allocator <ValueT>>& container)
{
typedef ContainerT<ValueT, std::allocator <ValueT>> type;
int id = 0;
for (auto& i : container)
i = id++;
}
I can write the function template Initialize() inside the class without any errors. 我可以在类内编写函数模板Initialize(),而不会出现任何错误。 But I want to write it outside.
但是我想在外面写。 Comments over my earlier post were helpful but I had to change my implementation
我之前的帖子中的评论很有帮助,但我必须更改实现
So in short what I want is: 1. write the generic function template over container type outside the class. 简而言之,我想要的是:1.在类之外的容器类型上编写通用函数模板。 2. what would be the prototype of that function inside the class ?
2.类中该函数的原型是什么?
Please suggest 请建议
Exactly the same as previously, only omit the body: 与以前完全相同,只是省略了主体:
template <class FOO>
class BAR {
public:
BAR : m_a (FOO::a), m_b (FOO::b), m_c (FOO::c) { }
template <template <typename, typename> class ContainerT, typename ValueT>
void Initialize(ContainerT <ValueT, std::allocator <ValueT>>& container);
// ... rest unchanged
};
// Definition outside:
template <class FOO> // for the class
template <template <typename, typename> class ContainerT, typename ValueT> // for the function
void BAR<FOO>::Initialize<ContainerT, ValueT>(ContainerT <ValueT, std::allocator <ValueT>>& container)
{
// same as before
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.