specialize insert function to pass by reference for non-fundamental types by using type traits

This commit is contained in:
Dennis Luxen 2015-01-22 11:02:49 +01:00
parent f662b9a081
commit 044271a55c

View File

@ -31,20 +31,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <functional> #include <functional>
#include <limits> #include <limits>
#include <queue> #include <queue>
#include <type_traits>
// max pq holds k elements // max pq holds k elements
// insert if key is smaller than max // insert if key is smaller than max
// if size > k then remove element // if size > k then remove element
// get() always yields a bound to the k smallest element in the stream // get() always yields a bound to the k smallest element in the stream
template<typename key_type> template <typename key_type> class upper_bound
class upper_bound
{ {
private:
using parameter_type =
typename std::conditional<std::is_fundamental<key_type>::value, key_type, key_type &>::type;
public: public:
upper_bound() = delete; upper_bound() = delete;
upper_bound(std::size_t size) : size(size) upper_bound(std::size_t size) : size(size) {}
{
}
key_type get() const key_type get() const
{ {
@ -55,7 +57,7 @@ class upper_bound
return queue.top(); return queue.top();
} }
void insert(const key_type key) void insert(const parameter_type key)
{ {
if (key < get()) if (key < get())
{ {