CPP引用std :: list中的指针

时间:2017-04-07 10:34:59

标签: c++ pointers reference stl compilation

以下代码不想编译:

#include <list>

int main()
{
  std::list<int *&> l;

  return 0;
}

它是一个*&amp;而不仅仅是*。 为什么?好吧,为什么这不起作用?我试图在互联网上寻找答案,但无法获得相关的东西。你能帮忙吗?

祝你有个美好的一天!

1 个答案:

答案 0 :(得分:1)

不允许使用容器类型的引用。但是你可以改用std :: reference_wrapper。

std::list<std::reference_wrapper<int>> l;

或者你可以使用指针指针

 std::list<int**> l;