Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:30:58+00:00 2026-06-18T04:30:58+00:00

I made a small ‘blocking queue’ class. It irritates me that I have created

  • 0

I made a small ‘blocking queue’ class. It irritates me that I have created redundant code for values passed into the enqueue member function.

Here are the two functions that do the same exact thing (except the rvalue uses std::move to move the rvalue into the actual queue collection), except handles lvalue and rvalue respectively:

    void enqueue(const T& item)
    {
        std::unique_lock<std::mutex> lock(m);
        this->push(item);
        this->data_available = true;
        cv.notify_one();
    }

    void enqueue(T&& item)
    {
        std::unique_lock<std::mutex> lock(m);
        this->push(std::move(item));
        this->data_available = true;
        cv.notify_one();
    }

My question is, is there a way to combine these two functions, without losing support for rvalue references.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:30:59+00:00Added an answer on June 18, 2026 at 4:30 am

    This is a classic example of the need to perfectly forward. Do this by templating the function (member template if this is a member function):

    template <class U>
    void enqueue(U&& item)
    {
        std::unique_lock<std::mutex> lock(m);
        this->push(std::forward<U>(item));
        this->data_available = true;
        cv.notify_one();
    }
    

    Explanation: If you pass an lvalue T to enqueue, U will deduce to T&, and the forward will pass it along as an lvalue, and you’ll get the copy behavior you want. If you pass an rvalue T to enqueue, U will deduce to T, and the forward will pass it along as an rvalue, and you’ll get the move behavior you want.

    This is more efficient than the “pass by value” approach in that you never make an unnecessary copy or move. The downside with respect to the “pass by value” approach is that the function accepts anything, even if it is wrong. You may or may not get cascaded errors down under the push. If this is a concern, you can enable_if enqueue to restrict what arguments it will instantiate with.

    Update based on comment

    Based on the comments below, this is what I understand things look like:

    #include <queue>
    #include <mutex>
    #include <condition_variable>
    
    template <class T>
    class Mine
        : public std::queue<T>
    {
        std::mutex m;
        std::condition_variable cv;
        bool data_available = false;
    public:
    
        template <class U>
        void
        enqueue(U&& item)
        {
            std::unique_lock<std::mutex> lock(m);
            this->push(std::forward<U>(item));
            this->data_available = true;
            cv.notify_one();
        }
    };
    
    int
    main()
    {
        Mine<int> q;
        q.enqueue(1);
    }
    

    This is all good. But what if you try to enqueue a double instead:

    q.enqueue(1.0);
    

    This still works because the double is implicitly convertible to the int. But what if you didn’t want it to work? Then you could restrict your enqueue like this:

    template <class U>
    typename std::enable_if
    <
        std::is_same<typename std::decay<U>::type, T>::value
    >::type
    enqueue(U&& item)
    {
        std::unique_lock<std::mutex> lock(m);
        this->push(std::forward<U>(item));
        this->data_available = true;
        cv.notify_one();
    }
    

    Now:

    q.enqueue(1.0);
    

    results in:

    test.cpp:31:11: error: no matching member function for call to 'enqueue'
            q.enqueue(1.0);
            ~~^~~~~~~
    test.cpp:16:13: note: candidate template ignored: disabled by 'enable_if' [with U = double]
                std::is_same<typename std::decay<U>::type, T>::value
                ^
    1 error generated.
    

    But q.enqueue(1); will still work fine. I.e. restricting your member template is a design decision you need to make. What U do you want enqueue to accept? There is no right or wrong answer. This is an engineering judgment. And several other tests are available that may be more appropriate (e.g. std::is_convertible, std::is_constructible, etc.). Maybe the right answer for your application is no constraint at all, as first prototyped above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a small log service that i want to publish to a
I made a small MVC Framework, but i need that my view Class can
I made a small and simple code that moves a div layer 200 pixles
I've made a small application for class that works like this: you click any
I have made a small custom view component: public class ActionBar extends RelativeLayout {
I have made a small java swing application that I want to share with
I made this small location class so that I can better handle address information
I've made a small game that uses level, and level is just a int
I have made a small application in Java and I would like to make
Problem: Have made a small mail program which works perfectly on my developer pc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.