【问题标题】:tr1: boost vs vs2010, using shared_ptr without namespacetr1:boost vs vs2010,使用没有命名空间的shared_ptr
【发布时间】:2012-03-10 04:03:28
【问题描述】:

试图用 vs2010 编译对 shared_ptr 有很多用途的旧项目。 所以,我已经预编译了头文件(stdafx.h):

..
使用命名空间标准; ..

#include "boost/shared_ptr"
使用命名空间提升;

后来在代码中我大量使用shared_ptr spObject;

我应该在 stdafx.h 中进行哪些更改,以便无需将代码中的任何地方 *shared_ptr* 替换为 *some_namespace::shared_ptr*?
是否可以避免 boost/boost::tr1/std::tr1/std 的命名空间冲突?

现在我有很多错误:

错误 C2872:“shared_ptr”:不明确的符号 可能是 'k:\boost10\boost\smart_ptr\shared_ptr.hpp(165) : boost::shared_ptr' 或 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(1418) : std::tr1::shared_ptr'

【问题讨论】:

  • 可能是不可能的?我看到的唯一变体是定义宏 #define shared_ptr std::shared_ptr
  • 现在我认为这在 c++ 中是不可能的(没有明确限定命名空间),太可悲了:(
  • 这是using namespace不好的原因之一。不要在以后的项目中使用它。或者至少只在 one 库上使用它。您仍然可以通过使用更有限的 using std::shared_ptr 等语句来避免大量命名空间限定。

标签: visual-studio-2010 boost namespaces shared-ptr tr1


【解决方案1】:

不要将using namespace 放在标题中,因为您发现它会破坏后面的标题,并且由于您无法更改这些标题,因此您无能为力。

在函数范围内,您可以使用 using 声明来消除歧义:

void f()
{
  using std::tr1::shared_ptr;
  shared_ptr<int> p;
}

但这在全局命名空间中不起作用,因为你已经用粗心的 using 指令污染了该范围。

【讨论】:

    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多