简体   繁体   English

const引用模板参数中的函数?

[英]const reference to a function in a template parameter?

How do I declare that I want a const function reference (in a template parameter)? 如何声明要使用const函数引用(在模板参数中)? eg, 例如,

template< bool (&func)(int arg) >
void foo(int stuff);

but const? 但是const?

More concretely, if I try to compile the following with icpc : 更具体地说,如果我尝试使用icpc编译以下内容:

template<bool (&func)(int arg)>
bool id(int arg) {
  return func(arg);
}

class Foo {
 public:
  Foo() {};
  virtual ~Foo() {};
  bool bar(int arg) { return true; }
  bool bar2(int arg) {
    return id<bar>(arg);
  };
};

int main() {
  return 0;
}

I get 我懂了

$ icpc foo.cpp
foo.cpp(12): error: no instance of function template "id" matches the argument list
            argument types are: (int)
      return id<bar>(arg);
             ^
compilation aborted for foo.cpp (code 2)

or, with g++ , I get 或者,使用g++ ,我得到

$ g++ foo.cpp
foo.cpp: In member function ‘bool Foo::bar2(int)’:
foo.cpp:13:23: error: no matching function for call to ‘id(int&)’
     return id<bar>(arg);
                       ^
foo.cpp:13:23: note: candidate is:
foo.cpp:2:6: note: template<bool (& func)(int)> bool id(int)
 bool id(int arg) {
      ^
foo.cpp:2:6: note:   template argument deduction/substitution failed:
foo.cpp:13:23: error: could not convert template argument ‘Foo::bar’ to ‘bool (&)(int)’
     return id<bar>(arg);
                       ^

But if instead I move bar to toplevel, as in 但是,如果我改为将栏移到顶层,例如

template<bool (&func)(int arg)>
bool id(int arg) {
  return func(arg);
}

bool bar(int arg) { return true; }

class Foo {
 public:
  Foo() {};
  virtual ~Foo() {};
  bool bar2(int arg) {
    return id<bar>(arg);
  };
};

int main() {
  return 0;
}

it compiles fine. 它编译良好。 Why does this happen, and how can I fix it without making bar a global? 为什么会发生这种情况?如何在不将bar设置为全局的情况下解决该问题?

Note: In my original code, I was getting "(not const-qualified) cannot be initialized with a value of type" errors: (with icpc ) 注意:在我的原始代码中,我得到“(不能用类型值初始化)(错误)”错误:(使用icpc

CollisionWorld.cpp(73): error: a reference of type "bool (&)(const Line &, vec_dimension={double}, vec_dimension={double}, vec_dimension={double}, vec_dimension={double})" (not const-qualified) cannot be initialized with a value of type "bool (const Line &, vec_dimension={double}, vec_dimension={double}, vec_dimension={double}, vec_dimension={double})"
    QuadTree<Line, vec_dimension, line_inside_box_with_time> *quad_tree =
                                  ^

(with g++ ) (使用g++

CollisionWorld.cpp:73:58: error: could not convert template argument ‘CollisionWorld::line_inside_box_with_time’ to ‘bool (&)(const Line&, double, double, double, double)’
   QuadTree<Line, vec_dimension, line_inside_box_with_time> *quad_tree =
                                                          ^

The problem is that the template expects a free function, not a member funciton. 问题在于模板需要自由功能,而不是成员功能。 That is why it works when you put bar() out of Foo, 这就是为什么当您将bar()放到Foo中时它起作用的原因,

Try it like this: 像这样尝试:

template<typename C, bool (C::*func)(int arg)>
bool id(C *mthis, int arg) {
  return (mthis->*func)(arg);
}

class Foo {
 public:
  Foo() {};
  virtual ~Foo() {};
  bool bar(int arg) { return true; }
  bool bar2(int arg) {
    return id<Foo, &Foo::bar>(this, arg);
  };
};

To call a member function, you need two things: the this pointer and the function. 要调用成员函数,您需要做两件事: this指针和函数。 Therefore it is not possible as easy as you write it. 因此,不可能像编写它那样容易。 id would need the this pointer! id将需要this指针!

The template definition would look like this: 模板定义如下所示:

template<bool (Foo::*func)(int)>

But still, you cannot implement a true id function that works both on functions and member functions. 但是,您仍然不能实现同时在函数和成员函数上起作用的true id函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM