Vit*_*Cpp 5 c++ templates template-meta-programming
我想实现一个编译时 foreach() ,它可以调用给定的模板成员函数 N 次。目前我有我的 foreach 编译时:
struct ForEach
{
template <size_t Dummy>
struct IntToType {};
typedef IntToType<true> ForEachDoNotTerminateLoop;
typedef IntToType<false> ForEachTerminateLoop;
template <size_t TIdx, size_t TCount, typename TMethod>
static void ForEachImpl(ForEachDoNotTerminateLoop, TMethod method)
{
method.Invoke<TIdx>();
ForEachImpl<TIdx + 1, TCount, TMethod>(Internal::IntToType<(TIdx + 1 < TCount)>(), method);
}
template <size_t TIdx, size_t TCount, typename TMethod>
static void ForEachImpl(ForEachTerminateLoop, TMethod method)
{
}
template <size_t TCount, typename TMethod>
static void Member(TMethod method)
{
ForEachImpl<0, TCount, TMethod>(Internal::IntToType<(0 < TCount)>(), method);
}
};
Run Code Online (Sandbox Code Playgroud)
还有一些模板类:
template <typename T, size_t TCount>
class SomeClass
{
public:
void Foo(int arg1)
{
ForEach::Member<TCount>(BarInvoker(this, arg1));
}
private:
struct BarInvoker // <-- How can I make this invoker a template to make it more flexible?
{
BarInvoker(SomeClass* instance, int arg1)
: instance(instance)
, arg1(arg1)
{}
template <size_t N>
void Invoke()
{
instance->Bar<N>(arg1);
}
int arg1;
SomeClass* instance;
};
template <size_t N>
void Bar(int arg1)
{
_data[N] = arg1;
}
int* _data;
T* _otherData;
};
Run Code Online (Sandbox Code Playgroud)
有没有办法绕过“调用者”函子,使其更灵活(模板)并且更易于使用?我真的不喜欢通过为每个私有成员函数添加“调用者”存根来使我的代码变得臃肿。如果能打电话就好了ForEach::Member<TCount, int>(Bar, 5);
预先感谢您帮助我解决这个模板的怪异问题!:)
将模板函数包装在一个类中,并将该类的实例传递给 ForEach::Call<> 并使用任意参数来调用它,似乎是一个相当干净的解决方案。
的ForEach::Call<>调用看起来像:ForEach::Call<N>(function_object, arguments...)
该类function_object的定义operator()如下:
template <std::size_t Idx> // Current index in the for-loop.
void operator()(/* arguments... */) { /* Impl. */ }
Run Code Online (Sandbox Code Playgroud)
这是我的 ForEach<> 版本。
class ForEach {
public:
/* Call function f with arguments args N times, passing the current index
through the template argument. */
template <std::size_t N, typename F, typename... Args>
static void Call(F &&f, Args &&...args) {
Impl<0, N>()(std::forward<F>(f), std::forward<Args>(args)...);
}
private:
/* Forward declaration. */
template <std::size_t Idx, std::size_t End>
class Impl;
/* Base case. We've incremeneted up to the end. */
template <std::size_t End>
class Impl<End, End> {
public:
template <typename F, typename... Args>
void operator()(F &&, Args &&...) { /* Do nothing. */ }
}; // Impl<End, End>
/* Recursive case. Invoke the function with the arguments, while explicitly
specifying the current index through the template argument. */
template <std::size_t Idx, std::size_t End>
class Impl {
public:
template <typename F, typename... Args>
void operator()(F &&f, Args &&...args) {
std::forward<F>(f).template operator()<Idx>(std::forward<Args>(args)...);
Impl<Idx + 1, End>()(std::forward<F>(f), std::forward<Args>(args)...);
}
}; // Impl<Idx, End>
}; // ForEach
Run Code Online (Sandbox Code Playgroud)
我写了一些类似于你的 SomeClass 的东西来演示它的用法。
template <std::size_t Size>
class Ints {
public:
using Data = std::array<int, Size>;
/* Call Assign, Size times, with data_ and n as the arguments. */
void AssignAll(int n) { ForEach::Call<Size>(Assign(), data_, n); }
/* Call Print, Size times, with data_ and n as the arguments. */
void PrintAll() const { ForEach::Call<Size>(Print(), data_); }
private:
/* Wraps our templated assign function so that we can pass them around. */
class Assign {
public:
template <size_t N>
void operator()(Data &data, int arg) const {
data[N] = arg;
}
}; // Assign
/* Wraps our templated print function so that we can pass them around. */
class Print {
public:
template <size_t N>
void operator()(const Data &data) const {
std::cout << data[N] << std::endl;
}
}; // Print
/* Our data. */
Data data_;
}; // Ints<Size>
Run Code Online (Sandbox Code Playgroud)
Ints 类的简单用例。
int main() {
Ints<5> ints;
ints.AssignAll(101);
ints.PrintAll();
}
Run Code Online (Sandbox Code Playgroud)
印刷:
101
101
101
101
101
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2984 次 |
| 最近记录: |