Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当对象类型为模板参数时,是否有方法将模板参数传递给对象上的函数?_C++_Templates_Function_Arguments_C++11 - Fatal编程技术网

C++ 当对象类型为模板参数时,是否有方法将模板参数传递给对象上的函数?

C++ 当对象类型为模板参数时,是否有方法将模板参数传递给对象上的函数?,c++,templates,function,arguments,c++11,C++,Templates,Function,Arguments,C++11,举例说明: struct MyFunc { template <size_t N> void doIt() { cout << N << endl; } }; template <typename Func> struct Pass123ToTemplateFunc { static void pass(Func f) { f.doIt<123>(); // <

举例说明:

struct MyFunc {

    template <size_t N>
    void doIt() {
        cout << N << endl;
    }

};

template <typename Func>
struct Pass123ToTemplateFunc {

    static void pass(Func f) {
        f.doIt<123>(); // <-- Error on compile; is there a way to express this?
    }

};

int main() {

    Pass123ToTemplateFunc<MyFunc>::pass(MyFunc());

    return 0;

}
struct MyFunc{
模板
void doIt(){

您必须告诉编译器,
doIt
将是一个模板:

f.template doIt<123>();
f.template doIt();

这个关键词的使用次数让我难以置信。谢谢!@nonoitall:有关更多信息,请参阅此常见问题解答: