This question already has an answer here:
Pass anonymous function object to std::function?
(1 answer)
Closed 7 years ago.
What I am trying to achieve is to make a functor that can take different functors as arguments.
Edit: the reason for my problem, the "most vexing parse", and the solution are well-described: see this question and answer, the whole most-vexing-parse tag, and even the wikipedia page. Still, I was not able to identify the problem before asking, and will leave this question as it might help others.
What I did:
In a header file functor.hpp:
#ifndef FUNCTOR_HPP
#define FUNCTOR_HPP
#include <functional>
template <typename T, typename BinOp = typename std::plus<T>>
struct doer {
BinOp op;
doer(BinOp o = std::plus<T>()) : op(o) {}
T operator()(const T& a, const T& b) const
{ return op(a, b); }
};
#endif // FUNCTOR_HPP
With this header, I can write a program functor.cpp like this:
#include <iostream>
#include "functor.hpp"
int main()
{
doer<int> f;
std::cout << f(3, 7) << std::endl;
}
and I can compile and run it to get, as expected:
$ make functor
g++ -std=c++14 -pedantic -Wall functor.cpp -o functor
$ ./functor
10
$
I am struggling to find a way to instantiate my doer with a different operator (not std::plus<T>).
doer<int, std::multiplies<int>> f2(std::multiplies<int>());
This compiles without a problem, but I have not been able to figure out a way to call f2(3, 7), to get the product 21. For example, if I add another line to the program:
int r = f2(3, 7);
and try to compile, I get:
$ make functor
g++ -std=c++14 -pedantic -Wall functor.cpp -o functor
functor.cpp: In function ‘int main()’:
functor.cpp:10:20: error: invalid conversion from ‘int’ to ‘std::multiplies<int> (*)()’ [-fpermissive]
int r = f2(3, 7);
^
functor.cpp:10:20: error: too many arguments to function ‘doer<int, std::multiplies<int> > f2(std::multiplies<int> (*)())’
functor.cpp:9:37: note: declared here
doer<int, std::multiplies<int>> f2(std::multiplies<int>());
^
functor.cpp:10:20: error: cannot convert ‘doer<int, std::multiplies<int> >’ to ‘int’ in initialization
int r = f2(3, 7);
^
What is going on? Seems almost like f2(3, 7) somehow is not calling the overloaded operator()...
Most vexing parse. Try this:
doer<int, std::multiplies<int>> f2((std::multiplies<int>()));
or this:
doer<int, std::multiplies<int>> f2 = std::multiplies<int>();
or this:
doer<int, std::multiplies<int>> f2{std::multiplies<int>()};
Related
I'm getting the following error
min.cpp:17:30: error: no viable conversion from '<overloaded function type>' to 'Container::UnaryFun' (aka 'function<double (double)>')
this->addFunction("abs", abs);
when trying to compile the following code:
#include <cmath>
#include <string>
#include <functional>
class Test
{
public:
using UnaryFun = std::function<double (double)>;
Test()
{
this->addFunction("abs", abs);
}
auto addFunction(const std::string& name, UnaryFun fun) -> void
{
// ...
}
};
auto main() -> int {
Test eval;
return 0;
}
I've tried to check the declaration of std::abs for argument double and return type double and looks like this:
inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) _NOEXCEPT {
return __builtin_fabs(__lcpp_x);
}
in /usr/local/Cellar/llvm/15.0.7_1/include/c++/v1/stdlib.h.
It is accesible specifically for the double type. I've checked this by adding:
double a = 5;
double b = std::abs(a);
and this compiles without problems or conversion warnings.
I've tried to declare my own abs function like so:
inline double xabs(double val)
{
return val < 0 ? -val : val;
}
and then change the following code like so to use this new xabs instead of std::abs
this->addFunction("abs", xabs);
and after this change, the code compiles.
Any ideas why the code with std::abs doesn't compile?
My environment:
OS: Mac OS 12.6
Compiler:
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Command to compile: g++ -std=c++2a -o min min.cpp
Update based on comments
I dug a bit deeper, and it seems that there is a problem with how std::function is declared, which led to the problem above.
If I declare addFunction like so, without std::function, the problem disappears.
auto addFunction(const std::string& name, double (*fun)(double)) -> void
{
}
This means that the compiler cannot figure out the matching abs if std::function is used but it can identify the matching overload if the type of the function is described directly without std::function.
The problem is that, since it has multiple overloads, std::abs doesn't have a single type. That means that the compiler can't select a std::function constructor to use to convert it since it can't deduce a type for the constructor's template parameter.
There are a couple of ways to get around that:
Use a cast:
addFunction("abs", std::static_cast<double(*)(double)>(std::abs));
Wrap it in a lambda:
addFunction("abs", [](double d) { return std::abs(d); });
As you've done, wrap it in a non-overloaded function
I want to set the array length to be the minimum of a constant and a generic like this:
template <int foo> struct Bar{
void my_func( int const (&my_array)[std::min(5, foo)] ) { /*...*/ }
};
This code compiles with clang++ but not g++ and I need my code to work with both. The error g++ gives is: error: array bound is not an integer constant before ']' token. How I can set the length of this array to be the minimum of foo and 5?
When I use clang++ I run into the problem that I can't get anything to bind to my_array. I want to run something like:
int main() {
static const int var[5] = {0,1,2,3,4};
Bar<5> bar;
bar.my_func(var);
}
But when I try to compile this code in clang++ I get: error: reference to type 'const int [*]' could not bind to an lvalue of type 'const int [5]'.
If I get rid of the std::min() stuff and replace it with foo the code compiles and runs fine.
Notes:
To get this code to compile you'll need to #include <algorithm> or similar to access std::min.
I don't think that this being part of a template should matter but when I try similar things with non-template function such as:
const int const_five = 5;
void new_func( int const (&my_array)[std::min(5,const_five)] ) { /*...*/ }
g++ says: error: variable or field 'new_func' declared void and clang++ says candidate function not viable: no known conversion from 'const int [5]' to 'const int [std::min(5, const_five)]' for 1st argument which both look like similar problems.
For int const (&my_array)[std::min(5, foo)] to compile, you need a version of std::min which is constexpr. It is since C++14.
Check the default value for -std of gcc and clang you use (its version-dependant). Ultimately, compile with -std=c++14.
Provided by StoryTeller, a nice working MCVE.
Keep it simple:
[foo < 5 ? foo : 5]
I have the following code
#include <boost/function.hpp>
#include <boost/bind.hpp>
class Foo {
public:
int getIfoo();
};
int Foo::getIfoo() {
return 5;
}
int main () {
boost::function<int (Foo)> getIntFoo;
getIntFoo = boost::bind( &Foo::getIfoo, _1 );
return 0;
}
When I compile with the following command g++ TestBoostBind.cpp I've got the following error
/includes/boost_1_60_0/boost/bind/mem_fn_template.hpp:35:36: error: invalid conversion from ‘const Foo*’ to ‘Foo*’ [-fpermissive]
BOOST_MEM_FN_RETURN (u.*f_)();
~~~~~~~^~
I'm confused about the source of the error whether it's originally from my code or the boost library. Does anyone know what the error means and how to fix it? I use g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 and boost.1.60
When binding to a member function, the first argument needs to be a pointer or a reference to the object to call the function on. It specifically can't be a value (an actual object instance). The boost::bind function have special cases for these two alternatives to generate the correct objects. It does not have any special case for passing by value.
Therefore you need to define getIntFoo as a function taking a pointer to Foo:
boost::function<int (Foo*)> getIntFoo;
Or a reference:
boost::function<int (Foo&)> getIntFoo;
You could try to use std::mem_fn to achieve the same goal:
Foo f;
std::function<int(Foo &)> getIntFoo = std::mem_fn(&Foo::getIfoo);
int ret = getIntFoo(f);
or if you need pointer argument, std::function could resolve this for you:
Foo f;
std::function<int(Foo *)> getIntFoo = std::mem_fn(&Foo::getIfoo);
int ret = getIntFoo(&f);
boost have its own alternative
I think my question is a corner case, of how far you can go with parameter pack expansion and it's a little hard to explain, what I want. The idea is easiest to express with a little code, so lets have a look at the following lines:
// <main.cpp>
#include <tuple>
#include <iostream>
// this function is supposed to return a std::tuple of function objects
// for each template argument type, there should be one such function object,
// that takes a value of this type and returns its half.
template <class... arg_t>
auto make_divide_by_two_lambda_tuple() {
return std::make_tuple(
[](const arg_t& elem) -> arg_t
{
return elem/2;
}...
);
}
int main()
{
// lets initialise a tuple with our function
auto value = make_divide_by_two_lambda_tuple<int, float>();
/*
* If everything workes as planned, the tuple has this structure
* value<0> := [](const int&){ ... }
* value<1> := [](const float&){ ... }
*/
// and we are able to use our functions in the expected way
std::cout << "5/2 (int version) = " << std::get<0>(value)(5) << std::endl;
std::cout << "5/2 (float version) = " << std::get<1>(value)(5) << std::endl;
return 0;
}
For clang(4.0.1), this example compiles and run as I expect it to do:
$ clang++ -std=c++14 ../main.cpp
$ ./a.out
5/2 (int version) = 2
5/2 (float version) = 2.5
For g++(7.1.1), this example does fail to compile:
$ g++ -std=c++14 ../main.cpp
../main.cpp: In function ‘auto make_divide_by_two_lambda_tuple()’:
../main.cpp:7:28: error: parameter packs not expanded with ‘...’:
[](const arg_t& elem) -> arg_t
^~~~~
../main.cpp:7:28: note: ‘arg_t’
../main.cpp:7:28: error: parameter packs not expanded with ‘...’:
../main.cpp:7:28: note: ‘arg_t’
../main.cpp:10:4: error: expansion pattern ‘<lambda>’ contains no argument packs
}...
^~~
../main.cpp: In function ‘int main()’:
../main.cpp:15:10: error: ‘void value’ has incomplete type
auto value = make_divide_by_two_lambda_tuple<int, float>();
So it seems, that gcc is willing to use a parameter pack inside the argument list of a lambda expression. But using the whole lambda is stretching it too far for gcc to accept.
Is this use of lambda expressions undefined behaviour, or is one compiler (hopefully gcc) wrong?
PS.: I also checked the docs, but it only explains, where you can use parameter pack expansion, and not what statments you can expand.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why do I need to use typedef typename in g++ but not VS?
Hi, recently I accounted with a "simple problem" of porting code from VC++ to gcc/intel.
The code is compiles w/o error on VC++:
#include <vector>
using std::vector;
template <class T>
void test_vec( std::vector<T> &vec)
{
typedef std::vector<T> M;
/*==> add here typename*/ M::iterator ib=vec.begin(),ie=vec.end();
};
int main()
{
vector<double> x(100, 10);
test_vec<double>(x);
return 0;
}
then with g++ we have some unclear errors:
g++ t.cpp
t.cpp: In function 'void test_vec(std::vector<T, std::allocator<_CharT> >&)':
t.cpp:13: error: expected `;' before 'ie'
t.cpp: In function 'void test_vec(std::vector<T, std::allocator<_CharT> >&) [with T = double]':
t.cpp:18: instantiated from here
t.cpp:12: error: dependent-name 'std::M::iterator' is parsed as a non-type, but instantiation yields a type
t.cpp:12: note: say 'typename std::M::iterator' if a type is meant
If we add typename before iterator the code will compile w/o pb.
If it is possible to make a compiler which can understand the code written in the more "natural way", then for me is unclear why we should add typename? Which rules of "C++ standards"(if there are some) will be broken if we allow all compilers to use without "typename"?
kind regards
Arman.
Here is a page that explains typename.
http://msdn.microsoft.com/en-us/library/8y88s595(VS.71).aspx
This will help you..