Home > Backend Development > C++ > body text

Why Does My Generic Function for Print Stacks and Queues Fail, and How Can I Fix It?

DDD
Release: 2024-11-01 22:17:02
Original
1018 people have browsed it

Why Does My Generic Function for Print Stacks and Queues Fail, and How Can I Fix It?

Why the Function Fails and How to Resolve the Issue

The provided code attempts to create a generic function that can print both stacks and queues. However, it fails due to a fundamental issue: both branches of the if-else statement must be compilable. In this case, they are not.

The error occurs specifically because the top() member function is only available for stacks, while the front() member function is only available for queues. The compiler flags the problem when trying to compile both branches for the same container type, as it cannot determine which member function to use.

One solution to this problem is to use partial specialization. Partial specialization allows you to define different implementations of a template for specific types or classes. In this case, you can define partial specializations for stacks and queues, ensuring that each implementation has the appropriate member function:

<code class="cpp">template <typename Cont>
struct element_accessor;

template <typename T>
struct element_accessor<std::stack<T>> {
    const T& operator()(const std::stack<T>& s) const { return s.top(); }
};

template <typename T>
struct element_accessor<std::queue<T>> {
    const T& operator()(const std::queue<T>& q) const { return q.front(); }
};

template<typename Cont>
void print_container(Cont& cont){
    while(!cont.empty()){
        auto elem = element_accessor<Cont>{}(cont);
        std::cout << elem << '\n';
        cont.pop();
    }
}
Copy after login

This solution uses a class element_accessor to access the appropriate member function for each container type. By providing partial specializations for stacks and queues, you ensure that the correct code is compiled for each case.

Another solution, available in C 17 and later, is to use if constexpr:

<code class="cpp">template<template<class> typename Cont, typename T>
void print_container(Cont<T>& cont){
    while(!cont.empty()){
        if constexpr (std::is_same_v<Cont<T>, std::stack<T>>) 
            std::cout << cont.top() << '\n';
        else if constexpr (std::is_same_v<Cont<T>, std::queue<T>>) 
            std::cout << cont.front() << '\n';
        cont.pop();
    }
}</code>
Copy after login

This solution utilizes constexpr conditional expressions to select the correct code based on the container type. By checking the type of Cont at compile time, you can ensure that the correct member function is called for each container.

The above is the detailed content of Why Does My Generic Function for Print Stacks and Queues Fail, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template