久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 std::is_same,為什么我的函數(shù)仍然不能用于

using std::is_same, why my function still can#39;t work for 2 types(使用 std::is_same,為什么我的函數(shù)仍然不能用于 2 種類型)
本文介紹了使用 std::is_same,為什么我的函數(shù)仍然不能用于 2 種類型的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我正在嘗試編寫一個(gè)可以打印堆棧和隊(duì)列的函數(shù),我的代碼如下

I am trying to write a function that can print both stack and queue, my code is as following

template<typename Cont>
void print_container(Cont& cont){
    while(!cont.empty()){
        if(std::is_same<Cont, stack<int>>::value){
            auto elem = cont.top();
            std::cout << elem << '
';
        } else {
            auto elem = cont.front();
            std::cout << elem << '
';
        }
        cont.pop();
        std::cout << elem << '
';
    }
}

int main(int argc, char *argv[])
{
    stack<int> stk;
    stk.push(1);
    stk.push(2);
    stk.push(3);
    queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);

    std::cout << "print stack" << endl;
    print_container(stk);
    std::cout << "print queue" << endl;
    print_container(q);

    return 0;
}

但是這里不起作用,錯(cuò)誤信息是:

demo_typeof.cpp:35:30: error: no member named 'front' in 'std::__1::stack<int, std::__1::deque<int, std::__1::allocator<int> > >'
            auto elem = cont.front();
                        ~~~~ ^
demo_typeof.cpp:52:5: note: in instantiation of function template specialization 'print_container<std::__1::stack<int, std::__1::deque<int, std::__1::allocator<int> > > >' requested here
    print_container(stk);
    ^
demo_typeof.cpp:32:30: error: no member named 'top' in 'std::__1::queue<int, std::__1::deque<int, std::__1::allocator<int> > >'
            auto elem = cont.top();
                        ~~~~ ^
demo_typeof.cpp:54:5: note: in instantiation of function template specialization 'print_container<std::__1::queue<int, std::__1::deque<int, std::__1::allocator<int> > > >' requested here
    print_container(q);
    ^
2 errors generated.

我知道這是有問(wèn)題的,并且知道 C++ 是靜態(tài)類型的并且沒(méi)有太多的運(yùn)行時(shí)支持.但我想知道這不起作用的具體原因,以及如何處理.

I know it's problematic, and know C++ is statically typed and without too much Runtime support. but I am wondering the specific reason why this doesn't work, and how to deal with it.

P.S.:判斷容器類型的實(shí)際含義是:你可以簡(jiǎn)單地通過(guò)傳遞隊(duì)列容器而不是堆棧來(lái)將DFS函數(shù)更改為BFS.因此,BFS 和 DFS 可以共享大部分代碼.

P.S.: The actual meaning of judge the typing of a container is that: You can simply change a DFS function into BFS by passing a queue container instead of a stack. So, BFS and DFS can share most of the code.

P.P.S:我在 C++ 11 環(huán)境中,但也歡迎對(duì)舊版或更新版標(biāo)準(zhǔn)的回答.

P.P.S: I am in C++ 11 environment, but answers for older or later standard are also welcomed.

推薦答案

if-else 語(yǔ)句的兩個(gè)分支都必須是可編譯的,而在您的情況下則不然.基于部分專業(yè)化的許多可能的解決方案之一,即使在 C++98 中也應(yīng)該工作:

Both branches of if-else statement must be compilable, which are not in your case. One of many possible solutions that is based on partial specialization and should work even in C++98:

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 << '
';
      cont.pop();
   }
}

<小時(shí)>

帶有 if constexpr 的 C++17 解決方案:


A C++17 solution with if constexpr:

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() << '
';
      else if constexpr (std::is_same_v<Cont<T>, std::queue<T>>) 
         std::cout << cont.front() << '
';
      cont.pop();
   }
}

這篇關(guān)于使用 std::is_same,為什么我的函數(shù)仍然不能用于 2 種類型的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問(wèn)向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問(wèn)沖突?)
主站蜘蛛池模板: 在线免费看a | 亚洲久久久| 黄色成人av | 黄色片免费在线观看 | 好好的日com| 国产一区二区三区在线观看视频 | 国产精品入口 | a级毛毛片 | 国产第三页 | 日韩爱爱视频 | 欧美亚洲 | 国产成人精品一区二区三区福利 | 国产中文字幕视频 | 欧美69视频 | 成人在线视频免费 | 黄色片免费网站 | 97精品久久 | 中文字幕在线观 | 欧美综合在线观看 | 一级片国产 | 国产丝袜视频 | 免费一区二区三区 | 久久久久久久久久国产 | 亚洲成人中文字幕 | 91色视频| 顶级黄色片 | 九九热在线观看视频 | 久久激情小说 | 国产在线视频一区二区 | 老司机午夜免费精品视频 | www国产视频 | av高清不卡| 成人午夜网 | 欧美成人一级片 | 伊人影院综合 | 国产精品久久免费 | 国产永久在线 | 日狠狠| 男人操女人视频网站 | 午夜黄色剧场 | 日本中文字幕一区 |