对模板参数中函数的常量引用
const reference to a function in a template parameter?
如何声明我想要一个常量函数引用(在模板中)参数)?例如,
template< bool (&func)(int arg) >
void foo(int stuff);
但是康斯特?
更具体地说,如果我尝试使用 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;
}
我得到
$ 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)
或者,有了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);
^
但是,如果我将栏移动到顶层,例如
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;
}
它编译得很好。为什么会发生这种情况,如何在不使 bar 成为全局的情况下修复它?
注意:在我的原始代码中,我收到"(非常量限定)无法使用类型值初始化"错误:(带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 =
^
(含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 =
^
问题是模板需要一个自由函数,而不是一个成员函数。这就是为什么当你把 bar() 从 Foo 中放出来时它会起作用的原因,
像这样尝试:
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);
};
};
要调用成员函数,您需要两样东西:this
指针和函数。因此,它不可能像你写的那么容易。 id
需要this
指针!
模板定义如下所示:
template<bool (Foo::*func)(int)>
但是,您仍然无法实现同时适用于函数和成员函数的真正id
函数。
相关文章:
- 什么时候在C++中返回常量引用是个好主意
- 通过常量引用传递参数的矩阵模板类
- 在C++中使用非常量引用作为常量
- 具有常量引用参数的函数模板专用化
- 多个"常量引用"变量可以共享同一个内存吗?
- 为什么 STL 容器适配器堆栈中的 top 返回常量引用?
- 为什么常量方法可以采用非常量引用?
- 为什么当我们有常量引用时创建临时对象?
- 如何返回向量的常量引用?
- C++:常量引用参数
- 不同于按值传递和常量引用传递的程序集
- 为什么const_cast和static_cast常量引用没有效果?
- C++ 获取函数在常量引用中按值返回的结果
- 从 BubbleSort* 类型的右值初始化 'AssortedSorter&' 类型的非常量引用无效"
- C++ 在类中使用常量引用文本时 O2 内存泄漏
- 是否可以跨 dll 边界返回常量引用/指向 std::vectors?
- C++中大多数/所有 setter 函数的参数是否应该写为常量引用?
- 通过非常量引用参数修改常量引用参数
- 将常量引用传递给线程
- 为什么C++中没有常量引用,就像常量指针一样?