2012-07-26 86 views
1

Visual Studio Enterprise 2010 sp1在Windows 7 64bit上。升压1.48.0。通过值认为参数是const

这里开始相关的代码。这些位在头中定义。

//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); 
}); 

我发现的错误是与拉姆达的参数列表。 swap被调用,我认为在这个对的拷贝构造函数中,试图首先从传递给lambda的右值指定给参数。编译器认为“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派生类。没有成员,没有办法,什么都没有。这足以在我的拷贝构造函数中导致这个错误。我无法想象如何隔离什么是实际违规代码行...

有没有人认为这是一个编译器打嗝,并且一些未提到的变化有这个副作用?

回答

6

value_typestd::map<K, V>std::pair<K const, V>,因为这些密钥是不可变的。所以,是的,value.first是const限定的。

+0

该死的。就这么简单。 现在,我可以将参数类型更改为std :: pair ,这很有吸引力,因为现在当我们从无符号短符号作为键移开时,我将不得不更改它... 但如果拷贝构造函数被调用,我的想法会变得很难理解为什么这仍然是相关的。 map实现如何使用常量键类型初始化它们自己的对? – 2012-07-26 18:19:43

+0

具有const限定成员的'pair'是可复制的,但不可赋值。 “地图”应该永远不需要将一对分配给另一个。 – 2012-07-26 18:28:07

相关问题