【问题标题】:Inheriting std::stack causes an error with clang but works with gcc继承 std::stack 会导致 clang 错误,但适用于 gcc
【发布时间】:2016-02-11 03:01:00
【问题描述】:

无关紧要。请参阅下面的更新。 我一直在玩 std::stack 并注意到一些奇怪的东西。代码如下:

.h 文件

template<typename Tp> using VecOfRef = std::vector<std::reference_wrapper<Tp>>;

template <typename T>
struct Stack : public std::stack<T,VecOfRef<T>> {};

struct Simple {
    std::string txt = "txt";
};

.cpp 文件

int main () {

    Simple smpl;

    auto vec = VecOfRef<Simple>{std::ref(smpl)};
    auto stdStack = std::stack<Simple,decltype(vec)>(vec); //works fine
    auto myStack = Stack<Simple>(vec); //error

    //to check if a reference is stored
    stdStack.push(smpl);
    smpl.txt.append("append");
    Simple& r = sStack.top();
    cout << r.txt << endl;

    return 0;
}

错误信息说:

19: 没有匹配的函数式转换 'std::__1::vector, std::__1::allocator > >' 到 '堆栈'

更新:

我一直在玩这个并且设法让代码几乎可以工作:

#include <vector>
#include <string>
#include <stack>
#include <iostream>
#include <functional>

template<typename Tp> using VecOfRef = std::vector<std::reference_wrapper<Tp>>;

template <typename T>
class Stack : public std::stack<T,VecOfRef<T>> {
public:
    using std::stack<T,VecOfRef<T>>::stack;
    using std::stack<T,VecOfRef<T>>::c;

    T& top() {

        return c.back().get();
    }
};


struct Simple {
    std::string txt = "txt";
    void print() { std::cout << txt << std::endl; }
};


int main() {

    Simple smpl;
    Simple smpl_2;
    Simple smpl_3;

    VecOfRef<Simple> vr {smpl,smpl_2,smpl_3};

//    auto myStack = Stack<Simple> (vr); // error
    auto myStack = Stack<Simple> ({smpl,smpl_2,smpl_3}); // ok
    auto stk = std::stack<Simple,std::vector<std::reference_wrapper<Simple>>>(vr); // ok_2

    smpl.txt.append("_append");
    smpl_2.txt.append("_append_2");
    smpl_3.txt.append("_append_3");

    myStack.top().print(); // txt_append_3
    myStack.pop();
    myStack.top().print(); // txt_append_2
    myStack.pop();
    myStack.top().print(); // txt_append

    return 0;
}

它在 gcc 下编译,但不在 clang 下。错误说:

错误:堆栈:154:43:'std::__1::enable_if' 中没有名为 'type' 的类型; 'enable_if' 不能用于禁用此声明

堆栈文件中的行:

【问题讨论】:

  • 很少有标准类是为公开继承而设计的。
  • 你试过auto myStack = Stack&lt;Simple&gt;(stdStack);
  • @Minato 拥有Stack 的意义在于不要使用std::stack(这都是为了教育目的)。

标签: c++ templates c++11 inheritance stack


【解决方案1】:

也许原因是你错过了一个构造函数

template <typename T>
struct Stack : public std::stack<T,VecOfRef<T>> 
{
    Stack(VecOfRef<T>){}
};

【讨论】:

  • 我要继承构造函数。
【解决方案2】:

您没有向 Stack 类添加任何构造函数。

如果你想继承 std:stack 的构造函数,你必须指定,使用: using std::stack&lt;T,VecOfRef&lt;T&gt;&gt;::stack; 在你的课堂上。

【讨论】:

  • 这敲响了一些钟声,但我不记得为什么这是必要的。此外,将此行放入Stack 类后,我在&lt;stack&gt; 文件中的template &lt;class _Alloc&gt; _LIBCPP_INLINE_VISIBILITY explicit stack(const _Alloc&amp; __a, typename enable_if&lt;uses_allocator&lt;container_type, _Alloc&gt;::value&gt;::type* = 0) : c(__a) {} 行中收到错误43: No type named 'type' in 'std::__1::enable_if&lt;false, void&gt;'; 'enable_if' cannot be used to disable this declaration
  • 我无法重现您的错误。请提供Short, Self Contained, Correct (Compilable), Example 之类的this 来说明问题。
  • 我在重现错误时遇到了困难。 here 似乎工作正常,但在 Xcode 中,stack header file 出现错误,上面写着 No type named 'type' in 'std::__1::enable_if&lt;false, void&gt;'; 'enable_if' cannot be used to disable this declaration
猜你喜欢
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多