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

  • Home
  • SEARCH
  • 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 8611651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:25:42+00:00 2026-06-12T04:25:42+00:00

I have a deque which is supposed to hold either struct a or struct

  • 0

I have a deque which is supposed to hold either struct a or struct b. The problem is one of the structures contains a pointer which I have to delete.

#pragma once

#include <iostream>
#include <deque>
#include <typeinfo>

struct packetUpdate {
    int recv;
    int packetID;
};

struct packet {
    int recv;
    int packetSize;
    char* m_packet;
};

template <class T>
class PacketQueue
{
private:
    int m_lastElement;
    int m_maxElementReached;
    int m_maxElements;

    std::deque<T> m_PacketList;

public:
    PacketQueue() { m_lastElement = -1; m_maxElements = 0; m_maxElementReached = 0; }
    ~PacketQueue() 
    { 
        if(typeid(T).name() == typeid(packet).name()) {
            for(int i = 0; i < m_lastElement+1; i++) {
                delete[] m_PacketList[i].m_packet;
            }
        }

        m_PacketList.~deque(); 
    }
};

This doesn’t work. The compiler tells me that packetUpdate doesn’t have a member m_packet. I understand why it is not working but the question is, is there a way to make this work without writing two different classes which look almost the same.

This is of course only a small selection of the class but it should illustrate my problem.

I guess I’m not the first one who had such a problem but since I’m not used to work with templates that much I didn’t know what I should look for.

  • 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-12T04:25:44+00:00Added an answer on June 12, 2026 at 4:25 am

    The absolut easiest way to do it is just to add destructor to packet. If that is not a possibility you can add a policy template to solve your problem:

    struct null_cleaner
    {
        template <typename T>
        void perform_clean(T& v)
        { /* do nothing */ }
    };
    
    template <typename PacketQueue, typename Cleaner = null_cleaner>
    class PacketQueue
    {
        ~PacketQueue ()
        {
            for (int i = 0; i < m_PacketList.size(); ++i) {
                Cleaner::perform_clean(m_PacketList[i]);
            }  
        }
    };
    

    Then when you add a type which needs cleaning you hand in a different Cleaner:

    // no cleaning
    PacketQueue<packetUpdate> q1;
    
    // requires cleaning
    struct packet_cleaner
    {
       void perform_clean(packet& p)
       {
           delete[] m_packet;
       }
    };
    
    // performs cleaning
    PacketQueue<packet, packet_cleaner> q2;
    

    Also there is no need for the specific call to m_PacketList.~deque() in fact it will just break your program because the compiler ensures it will be called anyway (so you are just calling it twice).

    Also the reason why you get compiler errors because m_packet doesn’t exist is that the if-block is compiled regardless of whether T == packet or T == packetUpdate and for packetUpdate there is no m_packet.

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

Sidebar

Related Questions

I have a Deque that contains this kind of stucts. struct New_Array { array<array<int,4>,4>
Problem I have a template container MyContainer<std::unique_ptr<Foo>> which has a std::deque<T> and a std::vector<T>
I have a deque that contains std::arrays . I want to convert it to
Lets say I have a structure struct s { std::deque<Object> q; //won't work with
Have deployed numerous report parts which reference the same view however one of them
In my question profiling: deque is 23% of my runtime i have a problem
I have a deque type list (a queue) which I'd like to show and
I have a class that i want to push_back into a deque. The problem
I have a std::deque, and I want to insert an element at a specified
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it

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.