Define priority_queue with lambda in header file












1















I have a question about defining a priority_queue pointer in header file and initializing it in source file.



header file



std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, ?> *readyQueue;


source file



auto readyQueueComparator = (const std::shared_ptr<Process> &first, const std::shared_ptr<Process> &second) {
return true;
};
readyQueue = new std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, decltype(readyQueueComparator)>;


Basically, I want to implement a structure like this but I couldn't figure out how should I define priority queue in header file in this format.



Thanks.










share|improve this question

























  • I wouldn't use a lambda here. I'd just write a functor. I'd also avoid having a global variable if it can be helped.

    – NathanOliver
    Nov 19 '18 at 21:24











  • That won't be possible with lambda, since it's type is unknown. On a side note, I am not sure if there is really a reason to have a pointer to priority_queue in the program.

    – SergeyA
    Nov 19 '18 at 21:28













  • @SergeyA Let's say that I won't use a pointer to priority_queue, but I still want that as a member variable in a class. (I didn't put the whole code, but ready_queue is actually a member variable.) How should I define priority_queue in header file in this case?

    – Mert Akozcan
    Nov 19 '18 at 21:58











  • @MertAkozcan pointer note was a side note. The actual answer is that you can't use lambda's type before you define the lambda. You can use a functor like shown in the answer.

    – SergeyA
    Nov 19 '18 at 22:00


















1















I have a question about defining a priority_queue pointer in header file and initializing it in source file.



header file



std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, ?> *readyQueue;


source file



auto readyQueueComparator = (const std::shared_ptr<Process> &first, const std::shared_ptr<Process> &second) {
return true;
};
readyQueue = new std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, decltype(readyQueueComparator)>;


Basically, I want to implement a structure like this but I couldn't figure out how should I define priority queue in header file in this format.



Thanks.










share|improve this question

























  • I wouldn't use a lambda here. I'd just write a functor. I'd also avoid having a global variable if it can be helped.

    – NathanOliver
    Nov 19 '18 at 21:24











  • That won't be possible with lambda, since it's type is unknown. On a side note, I am not sure if there is really a reason to have a pointer to priority_queue in the program.

    – SergeyA
    Nov 19 '18 at 21:28













  • @SergeyA Let's say that I won't use a pointer to priority_queue, but I still want that as a member variable in a class. (I didn't put the whole code, but ready_queue is actually a member variable.) How should I define priority_queue in header file in this case?

    – Mert Akozcan
    Nov 19 '18 at 21:58











  • @MertAkozcan pointer note was a side note. The actual answer is that you can't use lambda's type before you define the lambda. You can use a functor like shown in the answer.

    – SergeyA
    Nov 19 '18 at 22:00
















1












1








1








I have a question about defining a priority_queue pointer in header file and initializing it in source file.



header file



std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, ?> *readyQueue;


source file



auto readyQueueComparator = (const std::shared_ptr<Process> &first, const std::shared_ptr<Process> &second) {
return true;
};
readyQueue = new std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, decltype(readyQueueComparator)>;


Basically, I want to implement a structure like this but I couldn't figure out how should I define priority queue in header file in this format.



Thanks.










share|improve this question
















I have a question about defining a priority_queue pointer in header file and initializing it in source file.



header file



std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, ?> *readyQueue;


source file



auto readyQueueComparator = (const std::shared_ptr<Process> &first, const std::shared_ptr<Process> &second) {
return true;
};
readyQueue = new std::priority_queue<std::shared_ptr<Process>, std::vector<std::shared_ptr<Process>>, decltype(readyQueueComparator)>;


Basically, I want to implement a structure like this but I couldn't figure out how should I define priority queue in header file in this format.



Thanks.







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 21:23









NathanOliver

90.8k15126190




90.8k15126190










asked Nov 19 '18 at 21:21









Mert AkozcanMert Akozcan

6317




6317













  • I wouldn't use a lambda here. I'd just write a functor. I'd also avoid having a global variable if it can be helped.

    – NathanOliver
    Nov 19 '18 at 21:24











  • That won't be possible with lambda, since it's type is unknown. On a side note, I am not sure if there is really a reason to have a pointer to priority_queue in the program.

    – SergeyA
    Nov 19 '18 at 21:28













  • @SergeyA Let's say that I won't use a pointer to priority_queue, but I still want that as a member variable in a class. (I didn't put the whole code, but ready_queue is actually a member variable.) How should I define priority_queue in header file in this case?

    – Mert Akozcan
    Nov 19 '18 at 21:58











  • @MertAkozcan pointer note was a side note. The actual answer is that you can't use lambda's type before you define the lambda. You can use a functor like shown in the answer.

    – SergeyA
    Nov 19 '18 at 22:00





















  • I wouldn't use a lambda here. I'd just write a functor. I'd also avoid having a global variable if it can be helped.

    – NathanOliver
    Nov 19 '18 at 21:24











  • That won't be possible with lambda, since it's type is unknown. On a side note, I am not sure if there is really a reason to have a pointer to priority_queue in the program.

    – SergeyA
    Nov 19 '18 at 21:28













  • @SergeyA Let's say that I won't use a pointer to priority_queue, but I still want that as a member variable in a class. (I didn't put the whole code, but ready_queue is actually a member variable.) How should I define priority_queue in header file in this case?

    – Mert Akozcan
    Nov 19 '18 at 21:58











  • @MertAkozcan pointer note was a side note. The actual answer is that you can't use lambda's type before you define the lambda. You can use a functor like shown in the answer.

    – SergeyA
    Nov 19 '18 at 22:00



















I wouldn't use a lambda here. I'd just write a functor. I'd also avoid having a global variable if it can be helped.

– NathanOliver
Nov 19 '18 at 21:24





I wouldn't use a lambda here. I'd just write a functor. I'd also avoid having a global variable if it can be helped.

– NathanOliver
Nov 19 '18 at 21:24













That won't be possible with lambda, since it's type is unknown. On a side note, I am not sure if there is really a reason to have a pointer to priority_queue in the program.

– SergeyA
Nov 19 '18 at 21:28







That won't be possible with lambda, since it's type is unknown. On a side note, I am not sure if there is really a reason to have a pointer to priority_queue in the program.

– SergeyA
Nov 19 '18 at 21:28















@SergeyA Let's say that I won't use a pointer to priority_queue, but I still want that as a member variable in a class. (I didn't put the whole code, but ready_queue is actually a member variable.) How should I define priority_queue in header file in this case?

– Mert Akozcan
Nov 19 '18 at 21:58





@SergeyA Let's say that I won't use a pointer to priority_queue, but I still want that as a member variable in a class. (I didn't put the whole code, but ready_queue is actually a member variable.) How should I define priority_queue in header file in this case?

– Mert Akozcan
Nov 19 '18 at 21:58













@MertAkozcan pointer note was a side note. The actual answer is that you can't use lambda's type before you define the lambda. You can use a functor like shown in the answer.

– SergeyA
Nov 19 '18 at 22:00







@MertAkozcan pointer note was a side note. The actual answer is that you can't use lambda's type before you define the lambda. You can use a functor like shown in the answer.

– SergeyA
Nov 19 '18 at 22:00














1 Answer
1






active

oldest

votes


















3














The short answer is don't use a lambda for this. Define a custom callable type instead. For example:



struct OrderByProcessPriority
{
bool operator()(
std::shared_ptr<Process> const& first,
std::shared_ptr<Process> const& second) const;
}

using ProcessQueue = std::priority_queue<
std::shared_ptr<Process>,
std::vector<std::shared_ptr<Process>>,
OrderByProcessPriority>;

ProcessQueue *readyQueue;


Then, in your cpp file, create the instance like this:



readyQueue = new ProcessQueue{};





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382816%2fdefine-priority-queue-with-lambda-in-header-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The short answer is don't use a lambda for this. Define a custom callable type instead. For example:



    struct OrderByProcessPriority
    {
    bool operator()(
    std::shared_ptr<Process> const& first,
    std::shared_ptr<Process> const& second) const;
    }

    using ProcessQueue = std::priority_queue<
    std::shared_ptr<Process>,
    std::vector<std::shared_ptr<Process>>,
    OrderByProcessPriority>;

    ProcessQueue *readyQueue;


    Then, in your cpp file, create the instance like this:



    readyQueue = new ProcessQueue{};





    share|improve this answer




























      3














      The short answer is don't use a lambda for this. Define a custom callable type instead. For example:



      struct OrderByProcessPriority
      {
      bool operator()(
      std::shared_ptr<Process> const& first,
      std::shared_ptr<Process> const& second) const;
      }

      using ProcessQueue = std::priority_queue<
      std::shared_ptr<Process>,
      std::vector<std::shared_ptr<Process>>,
      OrderByProcessPriority>;

      ProcessQueue *readyQueue;


      Then, in your cpp file, create the instance like this:



      readyQueue = new ProcessQueue{};





      share|improve this answer


























        3












        3








        3







        The short answer is don't use a lambda for this. Define a custom callable type instead. For example:



        struct OrderByProcessPriority
        {
        bool operator()(
        std::shared_ptr<Process> const& first,
        std::shared_ptr<Process> const& second) const;
        }

        using ProcessQueue = std::priority_queue<
        std::shared_ptr<Process>,
        std::vector<std::shared_ptr<Process>>,
        OrderByProcessPriority>;

        ProcessQueue *readyQueue;


        Then, in your cpp file, create the instance like this:



        readyQueue = new ProcessQueue{};





        share|improve this answer













        The short answer is don't use a lambda for this. Define a custom callable type instead. For example:



        struct OrderByProcessPriority
        {
        bool operator()(
        std::shared_ptr<Process> const& first,
        std::shared_ptr<Process> const& second) const;
        }

        using ProcessQueue = std::priority_queue<
        std::shared_ptr<Process>,
        std::vector<std::shared_ptr<Process>>,
        OrderByProcessPriority>;

        ProcessQueue *readyQueue;


        Then, in your cpp file, create the instance like this:



        readyQueue = new ProcessQueue{};






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 21:31









        Peter RudermanPeter Ruderman

        10.2k2352




        10.2k2352






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382816%2fdefine-priority-queue-with-lambda-in-header-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?