Consider this program:
#include <iostream>
#include <stack>
class MyStack : public std::stack<double>
{
public:
void push(char)
{
std::cout << "Using char overload of push function.\n";
}
};
int main()
{
MyStack stack{};
stack.push(3.); // Why does this call push(char)?
}
This program compiles and prints "Using char overload of push function." when run. Why is it so?
I would think that the templated void push( value_type&& value );
would be used because value_type should be double and so should be better fit when calling push with the double argument 3.
I know I can use std::stack as a member and rewrite functions I need from std::stack to MyStack but inheritance with overloading seemed to be more elegant.
Compiler: gcc version 11.2.0 (Ubuntu 11.2.0-19ubuntu1)
Build command: g++-11 -std=c++20 -Wall -pedantic-errors -Wextra -Werror -Wsign-conversion -Weffc++ -o