4

I have the following .hpp file:

#ifndef CODE_HPP
#define CODE_HPP
#include <array>
#include <vector>
using std::vector;
using std::array;

template<typename T, typename C = vector<T>>
class stack;

template<typename T, typename C = vector<T>, typename K = stack<T,C>>
class stack_array;

template<typename T, typename C>
class stack
{
    C pile;

    stack();
    ~stack();
    void push(T&);
    friend class stack_array<T,C,stack<T,C>>;
};


template<typename T, typename C, typename K>
class stack_array
{
    private:
        static const size_t max_elem = 10;
        array<K, max_elem> store{};
        size_t index;
    public:
        stack_array(T&);
        ~stack_array();
};


template<typename T, typename C> stack<T,C>::stack(){
    pile.clear();
}

template<typename T, typename C> stack<T,C>::~stack(){
}

template<typename T, typename C> void stack<T,C>::push(T& _data){
    pile.push_back(_data);

    return;
}

template<typename T, typename C, typename K> stack_array<T,C,K>::stack_array(T& _data){

    index = 0;

    store[index].push(_data);
}

template<typename T, typename C, typename K> stack_array<T,C,K>::~stack_array(){
}

#endif

My .cpp file is reproduced below:

#include "code.hpp"
#include <iostream>
using std::cout;

int main (void){

    auto i = 0;
    stack_array<decltype(i)> s_a(i);

    return 0;
}

When the above code is compiled using the following command:

g++ -ggdb -std=c++17 -Wall main.cpp code.hpp

I get the following compilation error:

In file included from main.cpp:1:0:
code.hpp: In instantiation of ‘stack_array<T, C, K>::stack_array(T&) [with T = int; C = std::vector<int, std::allocator<int> >; K = stack<int, std::vector<int, std::allocator<int> > >]’:
main.cpp:8:35:   required from here
code.hpp:52:86: error: use of deleted function ‘std::array<stack<int, std::vector<int, std::allocator<int> > >, 10>::~array()’
 template<typename T, typename C, typename K> stack_array<T,C,K>::stack_array(T& _data){
                                                                                      ^
In file included from code.hpp:3:0,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/array:94:12: note: ‘std::array<stack<int, std::vector<int, std::allocator<int> > >, 10>::~array()’ is implicitly deleted because the default definition would be ill-formed:
     struct array
            ^~~~~

Why is the destructor for std::array getting invoked because of the code inside stack_array<T,C,K>::stack_array()?

6
  • that is intentional...the stack ctor() is getting invoked from within the friend class stack_array Commented Jul 23, 2019 at 7:53
  • 1
    how do you expect the array destructor to work if it can't access the destructor of the items in it ? More details : en.cppreference.com/w/cpp/language/… Commented Jul 23, 2019 at 7:59
  • presume that observation is correct,,,I got rid of the destructor for the stack class which wasn't doing much anyway, and the error disappeared... Commented Jul 23, 2019 at 8:06
  • if you didn't explicitly delete the destructor, there will be an implicitly defined (public) destructor in that case (which makes std::array happy). If you explicitly delete the destructor, you should get the same error. Commented Jul 23, 2019 at 8:10
  • I didn't explicitly delete the destructor using =delete; rather just let the compiler define a default destructor for stack by removing the declaration and definition altogether. And that got rid of the error. Commented Jul 23, 2019 at 8:13

1 Answer 1

4

You should add std::array as friend

template<typename T, typename C>
class stack
{
    C pile;

    stack();
    ~stack();
    void push(T&);
    friend class stack_array<T,C,stack<T,C>>;

    template< class TT, std::size_t N>  friend class std::array ;
};

demo : https://wandbox.org/permlink/TxAyRCvrwj9CtOhV

The message say : std::array<stack<int, std::vector<int, std::allocator<int> > >, 10>::~array()’ is implicitly deleted...

So you have an std::array of stack<smt>, std::array should be able to delete stack or its destructor is private => the destructor of array is implicitly deleted.

So you have to make it accessible to std::array by making it public or friend.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.