通过值认为参数是const

时间:2012-07-26 18:07:45

标签: c++ visual-c++ c++11 lambda boost-variant

Visual Studio Enterprise 2010,sp1,在Windows 7 64位上。提升1.48.0。

这里开始相关代码。这些位在标题中定义。

//typedef struct {} empty_t;
//typedef std::pair<size_t, std::shared_ptr<char>> string_t; //Don't ask...
//typedef boost::variant<empty_t, long, double, some other PODs, string_t> variant_t;
//typedef std::map<unsigned short, variant_t> variant_map_t;

,这是在复制构造函数的主体中:

std::for_each(std::begin(incoming.values_), std::end(incoming.values_), [&](variant_map_t::value_type value)
{
    // This guy is going to populate this::values_, doing the right thing -
    // copying by value native and POD types, or deep copying pointers.
    boost::apply_visitor(deep_copy_visitor(*this, value.first), value.second);
});

我找到的错误是lambda的参数列表。正在调用swap,我认为在该对的复制构造函数中,尝试首先从传递给lambda的rvalue分配给参数。编译器认为“value.first”在std :: pair复制构造函数中被赋值时是const。但显然,参数不是const限定的,mapped_type或key_type不是const限定的,复制构造函数不是const方法,并且没有任何东西应该是重要的。

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(209) : see reference to function template instantiation 'void std::_Swap_adl<_Ty1>(_Ty &,_Ty &)' being compiled
          with
          [
              _Ty1=unsigned short,
              _Ty=unsigned short
          ]
          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\utility(206) : while compiling class template member function 'void std::pair<_Ty1,_Ty2>::swap(std::pair<_Ty1,_Ty2> &)'
          with
          [
              _Ty1=const unsigned short,
              _Ty2=variant_t
          ]
          src\foo.cpp(506) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
          with
          [
              _Ty1=const unsigned short,
              _Ty2=variant_t
          ]

所以不知何故,模板参数得到了const限定,而对于我的生活,我无法弄清楚原因。

我认为其他的东西正在绊倒编译器,但我没有别的东西可以解决。早些时候,在我给我的源代码一个很好的轰动之前,试图解决这个问题,我可以打开和关闭此错误消息;我已经定义了一个boost :: static_visitor派生类。没有成员,没有方法,没有。这足以在我的拷贝构造函数中导致此错误。我无法想象如何隔离 ACTUALLY 令人讨厌的代码行......

是否有人认为这是一个编译器打嗝,有些未提及的更改会将此作为副作用?

1 个答案:

答案 0 :(得分:6)

value_type的{​​{1}}为std::map<K, V>,因为密钥是不可变的。所以,是的,std::pair<K const, V>是const限定的。