【发布时间】:2021-03-15 03:35:48
【问题描述】:
我想定义一个类成员std::set,函数指针为key_comp,但编译器报告“不是类型”。
bool compare(unsigned v1, unsigned v2)
{
...
}
std::set<unsigned, decltype(compare)*> GoodSet(compare);
class BadSet{
public:
...
std::set<unsigned, decltype<compare>*> Set2(compare);
};
int main()
{
BadSet S;
return 0;
}
GoodSet 编译正常,但 GNU C++ 在 BadSet 报告:“比较不是类型”。 我的系统是windows 10 + WSL 2.0 + ubuntu 20.04。
【问题讨论】:
-
对类成员初始化器使用大括号:
std::set<unsigned, decltype<compare>*> Set2{compare}; -
谢谢! Set2{compare} 有效。但是为什么类中的 std::set 定义需要不同的语法呢?
-
@wuyabiao -- 仔细看看你的尝试。如何构造一个需要使用括号(不是大括号)参数的类成员对象?您将需要使用成员初始化列表。另请注意,如果您有
struct B { B(int) {} };并尝试将B作为BadSet的类成员,您也会遇到同样的问题。所以问题与std::set无关。
标签: c++ function pointers stdset