1.先贴下出错代码
class Basket {
public:
void add_item(const std::shared_ptr<Quote> &sale);
double total_receipt(std::ostream&) const;
private:
static bool compare(const std::shared_ptr<Quote>& lhs,
const std::shared_ptr<Quote>& rhs)
{return lhs->isbn() < rhs->isbn();}
std::multiset<std::shared_ptr<Quote>,
decltype(compare)*> items (compare); //直接初始化出错
decltype(compare) *p = compare;//这里没有问题
};
error 提示:
error: ‘compare’ is not a type
decltype(compare)*> items (compare);
2.修改后的代码
class Basket {
public:
void add_item(const std::shared_ptr<Quote> &sale);
double total_receipt(std::ostream&) const;
private:
static bool compare(const std::shared_ptr<Quote>& lhs,
const std::shared_ptr<Quote>& rhs)
{return lhs->isbn() < rhs->isbn();}
std::multiset<std::shared_ptr<Quote>,
decltype(compare)*> items {compare}; // 列表初始化没有问题
decltype(compare) *p = compare;
};
3.在其他地方做得测试
using namespace std;
bool Less(const shared_ptr<Quote> &lhs, const shared_ptr<Quote> &rhs)
{
lhs->isbn() < rhs->isbn();
}
int main()
{
multiset<shared_ptr<Quote>, decltype(Less)*> items{Less};// items(Less) 和 items{Less}都可以通过编译
}
请问类的静态函数作为multiset的模板参数和初始换参数时有什么限制么?
http://www.cnblogs.com/tangzhenqiang/p/4332801.html
可以参考一下http://www.cnblogs.com/tangzhenqiang/p/4332801.html
感谢大家的回复,我已经找到答案了,
The idea is to flat out reject any syntax that could be interpreted as a function declaration. For example,
std::vector ns();
is a function declaration. These aren't:
std::vector ns{};
std::vector ns = std::vector();
For consistency, any member declaration with this form
T t(args...);
is disallowed, which avoids a repeat of the most vexing parse fiasco.
我的代码里
std::multisetstd::shared_ptr<Quote, decltype(compare)*> items (compare);
这个定义有可能被解释称函数了,但是compare又不是一个类型
所以error提示
error: ‘compare’ is not a type