Lambda for compare function in c++ not working - c++

This code produces an error:
priority_queue<int, vector<int>, [](int a, int b)->bool{return a>b;}> q;
Why?
(I know that for things like this I can just use std::greater or the default sorting, but I'm trying to learn how to create a custom comparator)
2 errors generated:
error: no matching function for call to object of type lambda
error: template argument for template type parameter must be a type

You need to specify the type, but not the lambda expression itself as the template argument. And lambda should be specified as constructor argument.
E.g.
auto c = [](int a, int b)->bool{return a>b;}; // declare lambda in advance
priority_queue<int, vector<int>, decltype(c)> q(c);
// ^^^^^^^^^^^ <- specify the type of lambda
// ^ <- specify the lambda as constructor argument

You can use std::function instead of lambda
auto q = std::priority_queue<int, std::vector<int>, std::function<bool(const int&, const int&)>>{
[](const int& a, const int& b)
{
return a < b;
}
};

Related

I need to implement a c++ function pointer to member function overload

arma::Mat<double> (*Sum)(arma::Mat<double>, int) = arma::sum; // The function pointer to arma::sum
arma::Mat<double> mat = Sum(A, 1); //A is of type arma::Mat<double>
std::cout<<mat;
produces the error :
error: no matches converting function ‘sum’ to type ‘class arma::Mat<double> (*)(class arma::Mat<double>, int)’
arma::Mat<double> (*Sum)(arma::Mat<double>, int) = &arma::sum;
I'm unable to understand this as
arma::Mat<T> mat;
mat = arma::sum(this->data, 1);
std::cout<<mat;
produces the desired result when the template T is of type double.Please help me out,thanks!!
The prototype for arma::sum in Armadillo documentation is given as:
sum( M )
sum( M, dim ) //dim = 0, 1 for rowise or columnwise sum
Just because the result can be assigned to a variable of a certain type, that doesn't mean that, that is the signature of the method.
A function can return a type which can be converted or assigned to a variable of another type.
In this case, to solve the problem - the original function has to be checked for it's signature (in the header files).
Then it will be possible to create the correct signature, expecially if there are overloads of the function.
For example, a signature from the Armadillo project:
template<typename T1>
arma_warn_unused
arma_inline
const Op<T1, op_sum>
sum
(
const T1& X,
const uword dim = 0,
const typename enable_if< is_arma_type<T1>::value == true >::result* junk1 = 0,
const typename enable_if< resolves_to_vector<T1>::value == false >::result* junk2 = 0
)
// ignore the implemenation ...
As can be seen this function returns an Op type which is a template class. The function has 2 usefull for the user parameters and 2 metaprogramming parameters, used by the implementation.
If you call this function with the type arma::Mat<double> then the signature of this chosen function is:
const Op<arma::Mat<double>, op_sum> (*sum_func)(const arma::Mat<double>, const uword, const void*, const void*)
As I can see in the header file, 11 definitions for sum function are provided. Then also metaprogramming is used to improve the performance of the implementation, which also increases the amount of combinations of parameters for this function, which means even more definitions.
So the type of this function is actually quite compilcated. And because of the metaprogramming it is not necassarily the one which is used, when it is called.
To help with the deduction of the return type decltype can be used.
class Test
{
public:
int sum (int i){return 1;}
float sum (float i){return 2.0;}
};
int main()
{
Test t;
decltype(t.sum(0)) (Test::* sum_ptr)(int) = &Test::sum;
return (t.*sum_ptr)(0);
}
I keep a helper function around just for this kind of cases, whare you want to resolve an overload without caring about the return type:
template <class... Params, class R>
auto ovl(R(*f)(Params...)) {
return f;
}
Your initialization then becomes:
auto Sum = ovl<arma::Mat<double>, int>(arma::sum);

Syntax to pass argument to unordered_set hash function in c++

I have created a hasher class for a custom type I'm using, but it has a constructor that takes an argument. I can't figure out the syntax to use this in an unordered_set.
class Hasher {
unsigned arg;
public:
Hasher(unsigned a) : arg(a) {}
size_t operator()(const MyType& t) const {
return calculate_hash(arg, t);
}
}
int main() {
unordered_set<MyType, Hasher(2)> myset; // compilation error
}
The error message:
In file included from Tetrahedron.cc:5:
./Triangulation.h:52:29: error: template argument for template type parameter must be a type
unordered_set<TetraFace,FaceHasher(2)> faces2;
^~~~~~~~~~~~~
/bin/../lib/gcc/x86_64-redhat-linux/6.3.1/../../../../include/c++/6.3.1/bits/unordered_set.h:90:11: note: template parameter is declared here
class _Hash = hash<_Value>,
^
I also tried
unordered_set<MyType, Hasher> myset(Hasher(2));
but I still get an error:
In file included from Tetrahedron.cc:5:
./Triangulation.h:52:59: error: expected ')'
unordered_set<TetraFace,FaceHasher> faces2(FaceHasher(2));
^
./Triangulation.h:52:58: note: to match this '('
unordered_set<TetraFace,FaceHasher> faces2(FaceHasher(2));
^
You're getting a compile error there because you're trying to pass an object (i.e. instance) of type Hasher as a template argument.
Like your error describes: template argument for template type parameter must be a type
It's expecting a type, and you're passing in a value.
Parameterize the arg at the type level.
template<unsigned A>
class Hasher {
unsigned arg = A;
public:
size_t operator()(const int& t) const {
std::cout << arg << std::endl;
return 0;
}
};
int main() {
std::unordered_set<int, Hasher<2>> myset;
myset.insert(5); // prints 2
std::unordered_set<int, Hasher<3>> myset2;
myset2.insert(3); // prints 3
}
Unfortunately it is not possible to construct a std::unorderd_set with just the hash object. All of the constructors that take the hash object have a parameter before it for bucket_count. You would need to specify the value for it like
unordered_set<MyType, Hasher> myset(some_bucket_count_value, Hasher(2));
If you do not want to do that then you have to make Hasher default constructable.
Also not that
return calculate_hash(arg);
Is not going to work as you will always hash arg no matter what MyType you pass. You need to be hashing the MyType object for the std::unordered_set to really work.

Default template argument for function ignored

template < class A, class B, class R = A >
void addMultiplyOperation( std::function< R ( const A&, const B& ) > func )
{
...
}
addMultiplyOperation< float, int >( []( float a, int b ) { return a * b; } );
This gives the compiler error:
In function 'int main(int, char**)':
error: no matching function for call to 'addMultiplyOperation(main(int, char**)::__lambda1)'
addMultiplyOperation< float, int >( []( float a, int b ) { return a * b; } );
^
note: candidate is:
note: template<class A, class B, class R> void addMultiplyOperation(std::function<R(const A&, const B&)>)
void addMultiplyOperation( std::function< R ( const A&, const B& ) > func )
^
note: template argument deduction/substitution failed:
note: 'main(int, char**)::__lambda1' is not derived from 'std::function<R(const float&, const int&)>'
addMultiplyOperation< float, int >( []( float a, int b ) { return a * b; } );
^
Despite having the R template argument default initialised to A, I have to provide the third argument in order for this to compile. Is there something else I have to do in order to use default template arguments?
I'm using g++ v4.8.1.
Despite having the R template argument default initialised to A, I have to provide the third argument in order for this to compile.
Actually, this has nothing to do with the fact that it's a default argument. The compiler can't deduce A and B either. Take a look at this simple example:
template<class A>
void f(function<void(A)> f) { }
int main() {
auto lambda = [](){};
f(lambda);
}
You'd think this would be super easy, and A should be deduced as void. But nope, it can't be done. When deducing template parameters, the compiler doesn't consider what constructors the parameter type would have for each possible combination of template parameters. It would be intractable to perform this sort of deduction in general.
For now, you'll just have to make addMultiplyOperation accept any type, and hope it's callable...
template<class Function>
void addMultiplyOperation(Function func) {
// ....
}
If necessary, there are ways to deduce the types of the arguments that the function object can accept, for example as described in this answer: Is it possible to figure out the parameter type and return type of a lambda?
This will lead to some nasty compilation errors if the object passed in is not actually callable, or takes the wrong number of arguments. For now I'm not sure whether there's a nice way to solve this. Concepts, coming in C++14, should alleviate some of these issues.

g++ compiler error: couldn't deduce template parameter ‘_Funct’

I'm trying to use an ANSI C++ for_each statement to iterate over and print the elements of a standard vector. It works if I have the for_each call a non-overloaded function, but yields a compiler error if I have it call an overloaded function.
Here's a minimal test program to show where the compiler error occurs:
#include <algorithm>
#include <iostream>
#include <vector>
struct S {
char c;
int i;
};
std::vector<S> v;
void print_struct(int idx);
void print_struct(const struct S& s);
// f: a non-overloaded version of the preceding function.
void f(const struct S& s);
int main()
{
v.push_back((struct S){'a', 1});
v.push_back((struct S){'b', 2});
v.push_back((struct S){'c', 3});
for (unsigned int i = 0; i < v.size(); ++i)
print_struct(i);
/* ERROR! */
std::for_each(v.begin(), v.end(), print_struct);
/* WORKAROUND: */
std::for_each(v.begin(), v.end(), f);
return 0;
}
// print_struct: Print a struct by its index in vector v.
void print_struct(int idx)
{
std::cout << v[idx].c << ',' << v[idx].i << '\n';
}
// print_struct: Print a struct by reference.
void print_struct(const struct S& s)
{
std::cout << s.c << ',' << s.i << '\n';
}
// f: a non-overloaded version of the preceding function.
void f(const struct S& s)
{
std::cout << s.c << ',' << s.i << '\n';
}
I compiled this in openSUSE 12.2 using:
g++-4.7 -ansi -Wall for_each.cpp -o for_each
The full error message is:
for_each.cpp: In function ‘int main()’:
for_each.cpp:31:48: error: no matching function for call to ‘for_each(std::vector<S>::iterator, std::vector<S>::iterator, <unresolved overloaded function type>)’
for_each.cpp:31:48: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from for_each.cpp:5:
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note: template<class _IIter, class _Funct> _Funct std::for_each(_IIter, _IIter, _Funct)
/usr/include/c++/4.7/bits/stl_algo.h:4436:5: note: template argument deduction/substitution failed:
for_each.cpp:31:48: note: couldn't deduce template parameter ‘_Funct’
I don't see any search results for this particular error on Stack Overflow, or on the web generally. Any help would be appreciated.
A names refers to an overload set. You'll need to specify which overload you want:
std::for_each(v.begin(), v.end(), (void (&)(S const&)) print_struct);
Another approach is to use a polymorphic callable function object as a helper:
struct PrintStruct
{
template <typename T> void operator()(T const& v) const
{ return print_struct(v); }
};
int main()
{
PrintStruct helper;
std::vector<S> sv;
std::vector<int> iv;
// helper works for both:
std::for_each(sv.begin(), sv.end(), helper);
std::for_each(iv.begin(), iv.end(), helper);
std::for_each declaration looks like this:
template<class InputIter, class Func>
void for_each(InputIter first, InputIter last, Func func);
As you can see, it takes anything you give it as the third parameter. There is no restriction that it has to be a callable type of a certain signature or a callable type at all.
When dealing with overloaded functions, they're inherently ambiguous unless you give them some context to select the right one. In a call to an overloaded function, this context are the arguments you pass. When you need a pointer, however, you can't use arguments as a context, and the for_each parameter also doesn't count as a context, since it takes anything.
As an example of where a function parameter can be a valid context to select the right overload, see this:
// our overloads
void f(int){}
void f(double){}
typedef void (*funcptr_type)(int);
void g(funcptr_type){}
// ...
g(&f); // will select 'void f(int)' overload, since that's
// the only valid one given 'g's parameter
As you can see, you give a clear context here that helps the compiler select the right overload and not have it ambiguous. std::for_each's parameters do not give such a context, since they take anything.
There are two solutions:
manually provide the context either by
casting to the right function pointer type, or
using an intermediate variable of the right type and passing that
use a non-overloaded function that dispatches to an overloaded one (as you did with f)
Note that in C++11, you could also use a lambda for the second option:
std::for_each(v.begin(), v.end(), [](const S& s){ print_struct(s); });
Some notes on your code:
(struct S){'a', 1} is a compound literal and not standard C++
you don't need struct S in C++, only S suffices

Using Lambdas in Maps

I'm trying to implement a map with a lambda function in C++11 as such
std::map<int, int, [](const int&a, const int& b) { return a < b; }> test;
but that fails with
error: type/value mismatch at argument 3 in template parameter list for ‘template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map’
error: expected a type, got ‘{}’
error: invalid type in declaration before ‘;’ token
Any advice?
You need to pass the type of the lambda as a template argument, not the lambda itself. What you want is this:
auto mycomp = [](const int&a, const int& b) { return a < b; };
std::map<int, int, decltype(mycomp)> test(mycomp);
Although in fact, since your lambda has no captures, it can actually be stored in a function pointer, so alternatively, you could do this:
std::map<int, int, bool(*)(const int&,const int&)>
test([](const int&a, const int& b) { return a < b; });
Though I find the first much more readable. Although using the function pointer type is more versatile. i.e. It can accept any function pointer or non-capturing lambda that matches that signature. But if you change your lambda to be capturing, it will not work. For a more versatile version, you could use std::function, i.e:
std::map<int, int, std::function<bool(const int&, const int&)>>
That will work with any function, lambda(capturing or not) or function object, as long as the signature matches.