Reputation: 1504
Why i get the marked compiler error (C2899)? I tried with VS2010 SP1.
#include <list>
#include <vector>
#include <algorithm>
template <typename source_container_type, typename target_container_type>
void copy_all(const source_container_type& source, target_container_type& target)
{
std::for_each(begin(source), end(source), [&] (const typename source_container_type::value_type& element)
{
// error C2899: typename cannot be used outside a template declaration
// error C2653: 'target_container_type' : is not a class or namespace name
target.push_back(typename target_container_type::value_type(element));
});
}
int main()
{
std::vector<int> a;
a.push_back(23);
a.push_back(24);
a.push_back(25);
std::list<int> b;
copy_all(a, b);
}
Kind regards
Simon
PS: I know i could use std::copy(..)
with std::back_inserter(..)
- but that's not the point.
EDIT
The question was answered in a comment by perreal: http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions
EDIT
Please note that i'm not interested in workarounds. I want to know whether the code above should compile or not.
Upvotes: 3
Views: 461
Reputation: 4025
Sorry don't have VS2010 available. Try moving the typedef outside of lambda. Works on g++.
#include <list>
#include <vector>
#include <algorithm>
template <typename source_container_type, typename target_container_type>
void copy_all(const source_container_type& source, target_container_type& target)
{
typedef typename target_container_type::value_type TargetType; /// Code change here.
std::for_each(source.begin(), source.end(), [&] (const typename source_container_type::value_type& element)
{
target.push_back(TargetType(element));
});
}
int main()
{
std::vector<int> a;
a.push_back(23);
a.push_back(24);
a.push_back(25);
std::list<int> b;
copy_all(a, b);
}
Upvotes: 1
Reputation: 18431
Since you are using C++0x/11, you can use:
target.push_back( (decltype(element))(element));
Upvotes: 0
Reputation: 34636
Your line is valid: http://ideone.com/qAF7r
Even the ancient g++ 4.3 compiles it. So it's probably a bug in your MS compiler.
Upvotes: 1
Reputation: 425
I may be misunderstanding what you are trying to achieve but shouldn't the line in question simply be:
target.push_back(element);
Upvotes: 0