Pass by value thinks parameter is const
-
12-12-2019 - |
Question
Visual Studio Enterprise 2010, sp1, on Windows 7 64bit. Boost 1.48.0.
Here begins the relevant code. These bits are defined in a header.
//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;
and this is in the body of a copy constructor:
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);
});
The error I'm finding is with the parameter list of the lambda. swap is being called, I think in the copy constructor of the pair, trying to assign first from the rvalue passed to the lambda to the parameter. The compiler thinks "value.first" is const when it's being assigned to in the std::pair copy constructor. But clearly, the parameter is not const qualified, the mapped_type or key_type isn't const qualified, the copy constructor isn't a const method, and NONE OF THAT SHOULD MATTER ANYWAY.
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
]
So somehow, the template parameter is getting const qualified, and for the life of me, I can't figure out why.
I think something else is tripping up the compiler, but I've got nothing else to go off of. Earlier, before I gave my source code a good stir, trying to figure this out, I could switch this error message on and off; I had defined a boost::static_visitor derived class. No members, no methods, nothing. That was enough to cause this error in my copy constructor here. I can't imagine how else to isolate what is ACTUALLY the offending line of code...
Does anyone think this is a compiler hiccup, and that some unmentioned change has this as a side effect?
Solution
The value_type
of std::map<K, V>
is std::pair<K const, V>
, because the keys are immutable. So, yes, value.first
is const-qualified.