1

If you create a stack with a container that doesn't meet the requirements of the required underlying container, you get a compile error. How is this an error that can be determined at compile-time vs. run-time.

For example:

#include <iostream>
#include <stack>

using namespace std;

class Test {
    int data;
};

int main()
{
    stack<int, Test> s;
    // s.push(5);
    cout<<"Hello World";

    return 0;
}

Will return the compile errors:

In file included from /usr/include/c++/6/stack:61:0,
                 from main.cpp:10:
/usr/include/c++/6/bits/stl_stack.h: In instantiation of ‘class std::stack<int, Test>’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',20)">main.cpp:20:22</span>:   required from here
/usr/include/c++/6/bits/stl_stack.h:102:46: error: no type named ‘value_type’ in ‘class Test’
       typedef typename _Sequence::value_type _Sequence_value_type;
                                              ^~~~~~~~~~~~~~~~~~~~
/usr/include/c++/6/bits/stl_stack.h:124:61: error: no type named ‘value_type’ in ‘class Test’
       typedef typename _Sequence::value_type                value_type;
                                                             ^~~~~~~~~~
/usr/include/c++/6/bits/stl_stack.h:125:61: error: no type named ‘reference’ in ‘class Test’
       typedef typename _Sequence::reference                 reference;
                                                             ^~~~~~~~~
/usr/include/c++/6/bits/stl_stack.h:126:61: error: no type named ‘const_reference’ in ‘class Test’
       typedef typename _Sequence::const_reference           const_reference;
                                                             ^~~~~~~~~~~~~~~
/usr/include/c++/6/bits/stl_stack.h:127:61: error: no type named ‘size_type’ in ‘class Test’
       typedef typename _Sequence::size_type                 size_type;
                                                             ^~~~~~~~~

I wanted to be able to enforce requirements for a template type and I was hoping to use how std::stack does it as a reference.

4
  • @FrançoisAndrieux good call on the stl vs std. Just corrected it. Jan 25, 2021 at 15:26
  • 1
    Note that there are ways to check various things regarding template type (C++20 concepts, SFINAE, static_assert with <type_traits> library, ...), but sometimes you can just go with "I assume type T will have these members available for me" and let the compiler detect any issues. Jan 25, 2021 at 15:28
  • 1
    "I wanted to be able to enforce requirements for a template type" -- it sounds like that may be your Real Question. If so, see the comment above this one. Jan 25, 2021 at 15:29
  • @DrewDormann thanks for clearing that up. Didn't want to merge two questions into one post, but that was my main intent of trying to figure this out. Jan 25, 2021 at 15:30

1 Answer 1

3

std::stack isn't actually doing anything. Inside std::stack, there is a like like

using value_type = Container::value_type;

and for your case, Container is Test and since Test::value_type does not exist, you get a compiler error.

1
  • Thanks so much for the quick reply! I'll accept once the time limit is up Jan 25, 2021 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.