==<>是什么意思?运营商?

时间:2015-08-06 12:53:40

标签: c++ templates c++11 operator-overloading

查看我的一位同事的一些代码,我得到了以下内容:

friend bool operator==<>(ValueIter<Type> const &rhs, ValueIter<Type> const &lhs);

它在模板类中声明:

template<typename Type>
class ValueIter: public std::iterator<std::bidirectional_iterator_tag, Type>

有人可以告诉我==<>符号表示什么吗?我希望它与!=运算符有关。

3 个答案:

答案 0 :(得分:32)

它看起来像两个,operator==是一个完整的模板实例化或专门化<>

我在野外只看到过这样的人。

鉴于friend,该类可能是befriending the template operator

如果您收到链接器错误,see this answer为什么。

答案 1 :(得分:6)

你的问题不完整。

据推测,在您正在检查的代码中的某些上下文中,存在模板Started POST "/poll_votes" for 127.0.0.1 at 2015-08-07 14:14:29 +0300 Processing by PollVotesController#create as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"XLXa9y1kE+RgDry20LxChoZ9iGA8XG5VroEcvRLQ0E0=", "poll"=>{"id"=>"46"}, "post"=>{"id"=>"25246"}, "poll_item"=>{"id"=>"85"}, "commit"=>"Голосовать"} User Load (2.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 30453 LIMIT 1 SQL (36.7ms) UPDATE `users` SET `last_request_at` = '2015-08-07 11:14:29' WHERE `users`.`id` = 30453 USER: dborovsky2 Poll Load (2.2ms) SELECT `polls`.* FROM `polls` WHERE `polls`.`id` = 46 LIMIT 1 PollItem Load (2.3ms) SELECT `poll_items`.* FROM `poll_items` WHERE `poll_items`.`poll_id` = 46 AND `poll_items`.`id` = 85 LIMIT 1 Post Load (2.5ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 25246 LIMIT 1 PollItem Load (2.7ms) SELECT `poll_items`.* FROM `poll_items` WHERE `poll_items`.`poll_id` = 46 (2.5ms) SELECT COUNT(*) FROM `poll_votes` WHERE (user_id=30453 AND poll_item_id IN (86,85)) (2.0ms) BEGIN User Load (2.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 30453 LIMIT 1 PollItem Load (2.6ms) SELECT `poll_items`.* FROM `poll_items` WHERE `poll_items`.`id` = 85 LIMIT 1 SQL (2.3ms) INSERT INTO `poll_votes` (`created_at`, `poll_item_id`, `updated_at`, `user_id`) VALUES ('2015-08-07 11:14:29', 85, '2015-08-07 11:14:29', 30453) SQL (2.9ms) UPDATE `poll_items` SET `poll_votes_count` = COALESCE(`poll_votes_count`, 0) + 1 WHERE `poll_items`.`id` = 85 (48.0ms) COMMIT Completed 200 OK in 136.5ms (Views: 0.3ms | ActiveRecord: 112.0ms | Sphinx: 0.0ms) 函数。

然后在某个类中,该模板化operator==()的特定特化被声明为operator==()

如果没有给出上下文(即前面的模板定义或封闭类定义),则无法给出更具体的答案。模板或相关专业化的可能性太多了。

答案 2 :(得分:5)

使用

template <typename T> class ValueIter;

template <typename T> 
bool operator==(ValueIter<T> const &rhs, ValueIter<T> const &lhs);

内部template <typename T> class ValueIter

  • friend bool operator==(ValueIter const &rhs, ValueIter const &lhs);
    friend bool operator==(ValueIter<T> const &rhs, ValueIter<T> const &lhs);
    将友谊添加到非模板操作员。

  • friend bool operator==<>(ValueIter const &rhs, ValueIter const &lhs);
    friend bool operator==<>(ValueIter<T> const &rhs, ValueIter<T> const friend bool operator==<T>(ValueIter const &rhs, ValueIter const &lhs);
    friend bool operator==<T>(ValueIter<T> const &rhs, ValueIter<T> const
    为模板操作符添加友谊(仅适用于匹配T)的类型

  • template <typename U> friend bool operator==(ValueIter<U> const &rhs, ValueIter<U> const &lhs);为模板操作符添加友谊(适用于任何类型UT可能不同))

==<>用于第二点,实际上是== <>