[英]How to implement a compile-time foreach() for template methods?
我想實現一個編譯時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);
}
};
還有一些模板類:
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;
};
有沒有辦法繞過“調用者”仿函數,使其更靈活(模板),更容易使用? 我真的不喜歡通過為每個私有成員函數添加“調用者”存根來膨脹我的代碼。 最好只調用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. */ }
這是我的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
我寫了類似於你的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>
Ints類的簡單用例。
int main() {
Ints<5> ints;
ints.AssignAll(101);
ints.PrintAll();
}
打印:
101
101
101
101
101
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.