c++ - 使用模板参数作为模板参数类型

标签 c++ templates template-function

下面的方式我好像无法使用多层模板,

template <typename T>
template <T value>
void printValueAsInteger()
{
    printf("value as integer is %i\n", (int) value);
}

这样它就可以被称为:

printValueAsInteger<123>();

它会导致以下错误消息:too many template-parameter-lists .

如果我使用 template <typename T, T value> 就可以了与 printValueAsInteger<int, 123>() ,但这需要我明确指定类型。我怎样才能做到printValueAsInteger<123>()版画 value as integer is 123

编辑:

我会更具体地说明我需要它做什么。我的目标是将成员函数作为函数指针传递,我想到了使用模板包装它:

template <typename T>
template <T* instance, void (T::*method)()>
void wrappedMethod()
{
    (instance->*method)();
}
void callFunction(void (*function)())
{
    (*function)();
}

然后像这样传递:

Base *instance = new Derived;
callFunction(&wrappedFunction<instance, Base::method>);

编辑:

呃,我刚刚意识到我可能不应该(也不能)使用运行时定义的变量作为模板参数。我现在正尝试使用一个用其他模板参数实例化的类来解决它,并创建一个使用该类的模板函数。或类似的东西。不,行不通。

请注意,我无法更改 callFunction 的签名,因为它是第三方 API 的一部分。

终于!

我将以下内容放在标题中,

class Callable
{
public:
    virtual ~Callable() { }
    virtual void call() { }
};

typedef void (*functionPtr)();
extern unsigned nextMethodId;
extern functionPtr wrappedMethods[];
extern Callable *boundMethods[];

template <unsigned I>
class MethodWrapper
{
public:
    static void function();
};

template <typename T>
class Method : public Callable
{
public:
    Method(T* instance, void (T::*method)());
    virtual void call();
private:
    T* instance;
    void (T::*method)();
};

template <typename T>
Method<T>::Method(T* instance, void (T::*method)())
    : instance(instance), method(method) {
}

template <typename T>
void Method<T>::call()
{
    if (instance && method)
        (instance->*method)();
}

template <typename T>
static functionPtr bindMethod(T* instance, void (T::*method)())
{
    boundMethods[nextMethodId] = new Method<T>(instance, method);
    return (void (*)()) wrappedMethods[nextMethodId++];
}

这在源文件中:

#include "<insert header name here>.h"

unsigned nextMethodId = 0;
functionPtr wrappedMethods[] = {
    &MethodWrapper<0>::function,
    &MethodWrapper<1>::function,
    &MethodWrapper<2>::function
};
Callable *boundMethods[sizeof(wrappedMethods) / sizeof(functionPtr)];

template <unsigned I>
void MethodWrapper<I>::function()
{
    boundMethods[I]->call();
}

我可以这样使用它:

Base *instance = new Derived;
void (*function)() = bindMethod(instance, &Base::method);
callFunction(function);

它成功调用派生实例的方法版本。遗憾的是,允许绑定(bind)的方法数量是固定的(在本例中为三个),但它很容易扩展。

最佳答案

一个简单的转换是将运行时 值作为一个仿函数构造函数的参数,该仿函数包含实例指针和指向成员函数的指针。使用处的语法将从:

Base *instance = new Derived;
callFunction(&wrappedFunction<instance, Base::method>);

收件人:

callFunction( WrappedFunction<Base,&Base::method>( instance ) );

WrappedFunction 类型的实现其实很简单,所以我把它留作练习。请注意,此方法的一个主要变化是 callFunction 的参数变成了一个仿函数,而不是一个函数指针。

在 C++11(或使用 boost)中,最简单的方法是不编写任何代码,只使用可用资源。将 callFunction 的签名更改为:

void callFunction( function<void ()> f );

然后使用bind 来调用:

callFunction( bind( &Base::method, instance ) );

关于c++ - 使用模板参数作为模板参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11603680/

相关文章:

C++ 用更多元素重写浮点 vector

c++ - 如何修改 boost 多边形?

ruby-on-rails-3 - 在 Rails 模板的末尾显示一条消息

c++ - C++ 函数模板特化是如何工作的?

c++ - 模板函数和类在不同文件中的使用

c# - 使用 boost 库在 C++ 中实现多线程的正确方法

c++ - Linux中getlogin函数的使用

c++ - 如何编写C++模板函数在内部调用多个C函数?

用于复杂类型的单一方法的 C++ 模板特化

c++ - 为什么ADL找不到函数模板?