| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2292 人关注过本帖
标题:multiset初始化问题(静态函数作为模板参数)
只看楼主 加入收藏
韩海0312
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-8-11
结帖率:83.33%
收藏
已结贴  问题点数:20 回复次数:5 
multiset初始化问题(静态函数作为模板参数)
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的模板参数和初始换参数时有什么限制么?


[此贴子已经被作者于2017-5-26 15:22编辑过]

搜索更多相关主题的帖子: 初始化 参数 const std compare 
2017-05-26 15:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
你的问题就是

int main( void )
{
    int a = 1;
    int b = { 2 };
    int c {3};
    int d( 4 );
}

struct foo
{
    int a = 1;
    int b = { 2 };
    int c {3};
    int d( 4 ); // 为什么这一句不允许吧?语法规定
};
2017-05-26 15:39
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
在C++标准中,brace-or-equal-initializer 是个专有名词
2017-05-26 16:00
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
语法为什么这规定,有人问过了
https://
https://
https://
2017-05-26 16:33
韩海0312
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-8-11
收藏
得分:0 
回复 4楼 rjsp
又是你,太感谢你了

我找到答案了
The idea is to flat out reject any syntax that could be interpreted as a function declaration. For example,
std::vector<int> ns();
is a function declaration. These aren't:
std::vector<int> ns{};
std::vector<int> ns = std::vector<int>();
For consistency, any member declaration with this form
T t(args...);
is disallowed, which avoids a repeat of the most vexing parse fiasco.
2017-05-26 19:30
韩海0312
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-8-11
收藏
得分:0 
回复 4楼 rjsp
我的代码里
std::multisetstd::shared_ptr<Quote, decltype(compare)*> items (compare);
这个定义有可能被解释称函数了,但是compare又不是一个类型
所以error提示
error: ‘compare’ is not a type
2017-05-26 19:35
快速回复:multiset初始化问题(静态函数作为模板参数)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026184 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved