remove_if() compilation error - c++

Compilation error in VS2010:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1840): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const triangle' (or there is no acceptable conversion)
h:\kingston_backup\ocv\ocv\delaunay.h(281): could be 'triangle &triangle::operator =(const triangle &)'
while trying to match the argument list '(const triangle, const triangle)'
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(1853) : see reference to function template instantiation '_FwdIt std::_Remove_if,_Pr>(_FwdIt,_FwdIt,_Pr)' being compiled
with
[
_FwdIt=std::_Tree_unchecked_const_iterator,std::allocator,true>>>,
_Mytree=std::_Tree_val,std::allocator,true>>,
_Pr=triangleIsCompleted
]
h:\kingston_backup\ocv\ocv\delaunay.cpp(272) : see reference to function template instantiation '_FwdIt std::remove_if,triangleIsCompleted>(_FwdIt,_FwdIt,_Pr)' being compiled
with
[
_FwdIt=std::_Tree_const_iterator,std::allocator,true>>>,
_Mytree=std::_Tree_val,std::allocator,true>>,
_Pr=triangleIsCompleted
]
I think the problem is in passing the arguments to the remove_if() of the STL, as suggested by the compiler error. I have added the following comment to the line:
//**** ERROR LINE
class triangleIsCompleted
{
public:
triangleIsCompleted(cvIterator itVertex, triangleSet& output, const vertex SuperTriangle[3])
: m_itVertex(itVertex)
, m_Output(output)
, m_pSuperTriangle(SuperTriangle)
{}
bool operator()(const triangle& tri) const
{
bool b = tri.IsLeftOf(m_itVertex);
if (b)
{
triangleHasVertex thv(m_pSuperTriangle);
if (! thv(tri)) m_Output.insert(tri);
}
return b;
}
};
// ...
triangleSet workset;
workset.insert(triangle(vSuper));
for (itVertex = vertices.begin(); itVertex != vertices.end(); itVertex++)
{
tIterator itEnd = remove_if(workset.begin(), workset.end(), triangleIsCompleted(itVertex, output, vSuper)); //**** ERROR LINE
// ...
}

remove_if does not remove anything (in the sense of erasing). It copies values around, so that all remaining values end up at the beginning of the range (and the rest of the range is in a more or less unspecified state).
Since keys in an associative container are immutable, it is not possible to copy values from one place to another within a set, so remove_if can't work for it.
The standard library does not seem to contain remove_if for set, so you'd have to roll your own. It might look like this:
#include <set>
template <class Key, class Compare, class Alloc, class Func>
void erase_if(std::set<Key, Compare, Alloc>& set, Func f)
{
for (typename std::set<Key, Compare, Alloc>::iterator it = set.begin(); it != set.end(); ) {
if (f(*it)) {
set.erase(it++); //increment before passing to erase, because after the call it would be invalidated
}
else {
++it;
}
}
}

Related

Cannot convert template when using lambda as parameter

I want to pass a lambda to a function, but I have run into a problem of successfully passing it onto the function. The function chooses to append TrueVal or FalseVal and creates a vector of boolean, based on the given condition.
I'm using 2019 Visual Studio's ISO C++14 Standard to compile the code.
#include <iostream>
#include <vector>
using namespace std;
template<typename T, typename T1, typename T2>
vector<bool> ConstructNestedVectorByElements(T condition, T1 TrueVal, T2 FalseVal) {
vector<bool> TempCol;
TempCol = {};
for (int i = 0; i < 3; i++)
{
if (condition(i)) {
TempCol.emplace_back(TrueVal);
}
else {
TempCol.emplace_back(FalseVal);
}
}
return TempCol;
}
int main()
{
vector<int> NumList = { 0, 1, 2 };
vector<bool> BoolList = {true, false, true};
auto ElementIsZero = [&NumList](int i) {return NumList[i] == 0; };
vector<bool> a = ConstructNestedVectorByElements(ElementIsZero, true, false); //this works
auto OriginalElement = [&BoolList](int i) {return BoolList[i]; };
vector<bool> b = ConstructNestedVectorByElements(ElementIsZero, true, OriginalElement); //error
return 0;
};
The error message:
C2440 'initializing': cannot convert from 'T2' to 'bool' ...\include\vector line 2385
1>...\vector(2385,18): error C2440: 'initializing': cannot convert from 'T2' to 'bool'
1> with
1> [
1> T2=main::<lambda_e116e485fb739b952327b9205614af81>
1> ]
1>...\vector(2385,18): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>...\Source.cpp(19): message : see reference to function template instantiation 'decltype(auto) std::vector<bool,std::allocator<bool>>::emplace_back<T2&>(T2 &)' being compiled
1> with
1> [
1> T2=main::<lambda_e116e485fb739b952327b9205614af81>
1> ]
1>...\Source.cpp(36): message : see reference to function template instantiation 'std::vector<std::vector<bool,std::allocator<bool>>,std::allocator<std::vector<bool,std::allocator<bool>>>> ConstructNestedVectorByElements<main::<lambda_52b07f243bfcbbd5a342ddead4700eca>,bool,main::<lambda_e116e485fb739b952327b9205614af81>>(T,T1,T2)' being compiled
1> with
1> [
1> T=main::<lambda_52b07f243bfcbbd5a342ddead4700eca>,
1> T1=bool,
1> T2=main::<lambda_e116e485fb739b952327b9205614af81>
1> ]
template <class... _Valty>
decltype(auto) emplace_back(_Valty&&... _Val) {
bool _Tmp(_STD forward<_Valty>(_Val)...);
push_back(_Tmp);
I think the problem might be one of the following:
I'm passing more than one type of argument into T2 (a lambda and a bool): Perhaps I used the wrong keyword, typename, to initialize T2? I tried with class but the same thing occurred.
OriginalElement isn't given parameters when it requires them: This confuses me a bit. If I change the line to:
TempCol.emplace_back(FalseVal(i, j)); //this is line 19
This error shows up:
C2064 term does not evaluate to a function taking 2 arguments ...\Source.cpp line 19
However, this seems not to be the case for condition(i, j), which compiles correctly. Is there a difference in handling (what I assume to be) boolean when in a conditional, and when appending it to a vector?
Lambdas not being constexpr, so it can't be used in templates: I don't really understand it, but there seems to be some relationship with this topic: (1, 2, 3)
The issue is that OriginalElement is not a bool and cannot be implicitly converted to one. You can call it to get a bool by passing an int. Change this line in the template:
TempCol.emplace_back(FalseVal(i));
then
auto OriginalElement = [&BoolList](int i) {return BoolList[i]; };
vector<bool> b = ConstructNestedVectorByElements(ElementIsZero, true, OriginalElement);
Compiles fine, but passing false will not work, because false is not a callable. T2 cannot be both, a callable and a bool. If you want both instantiations to work, you can change the first to
auto ElementIsZero = [&NumList](int i) {return NumList[i] == 0; };
vector<bool> a = ConstructNestedVectorByElements(ElementIsZero, true, [](int){ return false;});
Complete Demo
However, I suggest you to write two overloads. One that takes a callable. And to avoid the overhead of the callable, another one that takes a plain bool (no template parameter, let the conversion happen on the caller).

C++ Template Function specialization error

I am new in using C++ templates.
I need to write a template function specialization for my project.
It is a simple Sum function for different type inputs and it calculates the sum between two iterators. The original function is generic and so accepts a template argument. The template specialization is written for Maps.
#include <map>
#include <string>
template <typename T>
double Sum(T &it_beg, T &it_end) {
double sum_all = 0;
for(it_beg++; it_beg != it_end; it_beg++)
sum_all += *it_beg;
return sum_all;
};
template <>
double Sum(std::map<std::string, double> &it_beg, std::map<std::string, double> &it_end) {
double sum_all = 0;
for(it_beg++; it_beg != it_end; it_beg++)
sum_all += it_beg->second;
return sum_all;
};
when I try to run the code, I get the following errors
...\sum.h(21): error C2676: binary '++' : 'std::map<_Kty,_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> _Kty=std::string,
1> _Ty=double
1> ]
I appreciate if anyone could give me a hint !
thanks
Your function signature should look like this (possibly without references) so you can pass in rvalues (iterators are cheap to copy anyway):
template <>
double Sum(std::map<std::string, double>::iterator it_beg,
std::map<std::string, double>::iterator it_end)
std::map does not define operator++, clearly your arguments are meant to be std::map::iterators.
Don't forget to remove the references from the main template function parameters too.
There's also this:
for(it_beg++; it_beg != it_end; it_beg++)
Why are you incrementing it_beg as you enter the loop? You can leave initialization statement empty.

Is it possible to get a template to use a class and a member function of that class?

I'm trying to make a for_each function for a generic object that uses an size function and an item index function. But I'm having some difficulty with the syntax.
This is what I have so far (starting at line 128):
class base1
{
protected:
std::vector<int> items;
public:
base1()
: items({1,2,3})
{
}
int GetCount() const
{
}
};
class base2 : public base1
{
public:
base2()
: base1()
{
}
int GetItem(int i) const
{
return items[i];
}
};
class derived : public base2
{
public:
derived()
: base2()
{
}
};
template <typename CONTAINER, typename CONTAINER_BASE1, typename CONTAINER_BASE2, typename SIZE, typename CONTAINED, typename FUNC>
void for_each(CONTAINER* container, SIZE (CONTAINER_BASE1::*GetSize)() const, CONTAINED (CONTAINER_BASE2::*GetItem)(SIZE) const, FUNC& body)
{
for (SIZE i = 0; i < container->*GetSize(); ++i)
{
body(container->*GetItem(i));
}
}
void fn()
{
derived x;
for_each(&x, &derived::GetCount, &derived::GetItem, [](int i){
++i;
});
}
Right now, I get an error from VC++ 2013 stating:
1>d:\projects\test\test.cpp(169): error C2064: term does not evaluate to a function taking 0 arguments
1> d:\projects\test\test.cpp(180) : see reference to function template instantiation 'void for_each<derived,base1,base2,int,int,fn::<lambda_862ea397905775f7e094cde6fe9b462c>>(CONTAINER *,SIZE (__thiscall base1::* )(void) const,CONTAINED (__thiscall base2::* )(SIZE) const,FUNC &)' being compiled
1> with
1> [
1> CONTAINER=derived
1> , SIZE=int
1> , CONTAINED=int
1> , FUNC=fn::<lambda_862ea397905775f7e094cde6fe9b462c>
1> ]
1>d:\projects\test\test.cpp(171): error C2064: term does not evaluate to a function taking 1 arguments
Any ideas as to what the problem is?
You have two bugs. You take the functor by non-const lvalue reference - FUNC& body - which doesn't bind to a temporary like a lambda; this was hidden by a terrible MSVC extension that allows such bindings. You should accept the function object by value (the way it is usually done by the standard library), by const lvalue reference (if copying is expensive and/or identity is important), or by forwarding reference (if identity is important and operator() can be non-const).
Second is operator precedence. The postfix function call operator has higher precedence than .* and ->*. container->*GetSize() is container->*(GetSize()); you want (container->*GetSize)().
I'm also not sure about this design. It's probably better to provide a uniform interface, and simply do, e.g., container.size() and container.at(i) than using this tortured system of pointer-to-member-functions.

How can I improve this template to accept lambdas in VS2012

The following is cute little template that I use often. Simply tells me if the given element is a member of a collection (which itself must be compatible with the find_if template):
// returns true if a given collection contains the given element
// NOTE: This is NOT optimized for associative containers!
template <typename ELEMENT, typename COLLECTION, typename PREDICATE>
bool contains(const COLLECTION & collection, ELEMENT element, PREDICATE predicate)
{
return collection.end() != std::find_if(collection.begin(), collection.end(), boost::bind(predicate, element, _1));
}
I'm finding that VC2012 balks if I try to use a lambda as the predicate:
if (!contains(specs, str, [] (CString pathname, CString pattern) { return AsBool(PathMatchSpec(pathname, pattern)); }))
continue;
VS2012SP1 spits out the following for the above context:
1>c:\users\steve\projects\cimex cad-cam\15.0\3rd party\boost\boost\bind\bind.hpp(69): error C2039: 'result_type' : is not a member of 'CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>'
1> c:\users\steve\projects\cimex cad-cam\15.0\cimex application\cimcad\macro directory.cpp(166) : see declaration of 'CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>'
1> c:\users\steve\projects\cimex cad-cam\15.0\3rd party\boost\boost\bind\bind_template.hpp(15) : see reference to class template instantiation 'boost::_bi::result_traits<R,F>' being compiled
1> with
1> [
1> R=boost::_bi::unspecified,
1> F=CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>
1> ]
1> c:\users\steve\projects\cimex cad-cam\15.0\mfc toolbox\miscellaneous.h(360) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
1> with
1> [
1> R=boost::_bi::unspecified,
1> F=CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>,
1> L=boost::_bi::list2<boost::_bi::value<CString>,boost::arg<1>>
1> ]
1> c:\users\steve\projects\cimex cad-cam\15.0\cimex application\cimcad\macro directory.cpp(166) : see reference to function template instantiation 'bool contains<CString,substring_container_adapter,CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>>(const COLLECTION &,ELEMENT,PREDICATE)' being compiled
1> with
1> [
1> COLLECTION=substring_container_adapter,
1> ELEMENT=CString,
1> PREDICATE=CMacroInterpreter::GetDirectoryOf::<lambda_60eac39ee69a5bdc77e08d06d79ae4c4>
1> ]
I'm unclear on how to coerce things to accept the predicate lambda. Seems that boost is unable to deduce the return type of the lambda. And I'm unclear on what I can do to fix that?
I could define a local std::binary_function derivative functor. Just seems like it would be better to fix contains<> to allow it to handle lambdas directly.
It seems to be an issue with boost::bind. Using std::bind instead, your code builds fine with lambdas in VS2012:
#include <functional>
#include <algorithm>
#include <vector>
template <typename ELEMENT, typename COLLECTION, typename PREDICATE>
bool contains(const COLLECTION & collection, ELEMENT element, PREDICATE predicate)
{
return collection.end() != std::find_if(collection.begin(), collection.end(), std::bind(predicate, element, std::placeholders::_1));
}
std::vector<int> a;
int main()
{
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(42);
bool c = contains(a, 42, [](int a, int b) { return a == b; });
return 0;
}
The same code builds just fine with g++ as well.
You could always try using another lambda:
template <typename ELEMENT, typename COLLECTION, typename PREDICATE>
bool contains(const COLLECTION & collection, ELEMENT element, PREDICATE predicate)
{
typedef typename COLLECTION::value_type VALUE;
return collection.end() != std::find_if(collection.begin(), collection.end(),
[&]( VALUE const & e ){ return predicate( element, e ); });
}

Is this a bug in Visual C++ 2010, or am I missing something?

Given the following code:
#include <vector>
template<class C1, class C2, class Op>
std::vector<typename Op::result_type>
f(Op op, const C1& src1, const C2& src2)
{
}
template<class It, class Op>
std::vector<typename Op::result_type> g(Op op, It begin, It end)
{
}
template<class It1, class It2, class Op>
std::vector<typename Op::result_type> g(Op op, It1 left_begin, It1 left_end, It2 right_begin)
{
return std::vector<typename Op::result_type>();
}
struct ToS
{
typedef double result_type;
double operator() (long , double ) const { return 0.0; }
};
std::vector<double> h(std::vector<long> const& vl, std::vector<double> const& vd)
{
return g(ToS(), vl.begin(), vl.end(), vd.begin());
}
When compiled with Visual C++ 2010 (SP1), I get the following errors:
1>VC10Error.cpp(30): error C2893: Failed to specialize function template 'std::vector<Op::result_type> g(Op,It1,It1,It2)'
1> With the following template arguments:
1> 'std::_Vector_const_iterator<_Myvec>'
1> with
1> [
1> _Myvec=std::_Vector_val<long,std::allocator<long>>
1> ]
1> 'std::_Vector_const_iterator<_Myvec>'
1> with
1> [
1> _Myvec=std::_Vector_val<double,std::allocator<double>>
1> ]
1> 'ToS'
1>VC10Error.cpp(30): error C2780: 'std::vector<Op::result_type> g(Op,It,It)' : expects 3 arguments - 4 provided
1> VC10Error.cpp(12) : see declaration of 'g'
I don't understand them. First, of course, the error message basically
sums up as "There's something wrong here, but we won't tell you what'.
And secondly, I don't find anything wrong; nor does g++ (version 4.4.2).
Other interesting symptoms: if you add a using std::vector; after the
include, and delete all of the std::, it works—I would have
thought that that should have no effect. And if you delete either the
function f (which really isn't used anywhere) or the first version of
function g, it also works.
So am I crazy, or is VC10 really not yet production-ready?
EDITED: Just to add: if it is a bug in the compiler, how do I reliably work around it?
Indeed it appears a bug in the compiler.
In your simplified example, the issue goes away if the two versions of g() exchange places, or if f() is commented out, or f() exchange places with g<It,Op>(Op, It, It).
It works fine with g++ compiler. So it's possible that VC++ is not able to parse through it properly (may be a bug in that case). Your code is proper.