No matching function call error with usage of, lambda expressions and std::function.
I am trying to use this lambda expression in my code. And looks like something's wrong with definition of functor or lambda.
I believe the lambda expression is true. but I can't satisfy the prototype of function I defined.
Function definition:
template<typename E, typename container>
void for_each(Iterator<E,container> begin, std::function<void(E&)> fun)
{
do // does the given unary functor to elements
{ //from starting iterator 'til the end.
fun(*begin);
begin = begin.next();
}while(begin.hasNext());
fun(*begin);
}
And caller:
for_each(c.iterator(), [&](E& e){add(e);});
I except achieve this function call with lambda expression. But compiler says "error: no matching function for call to .."
c++ c++11 lambda
add a comment |
I am trying to use this lambda expression in my code. And looks like something's wrong with definition of functor or lambda.
I believe the lambda expression is true. but I can't satisfy the prototype of function I defined.
Function definition:
template<typename E, typename container>
void for_each(Iterator<E,container> begin, std::function<void(E&)> fun)
{
do // does the given unary functor to elements
{ //from starting iterator 'til the end.
fun(*begin);
begin = begin.next();
}while(begin.hasNext());
fun(*begin);
}
And caller:
for_each(c.iterator(), [&](E& e){add(e);});
I except achieve this function call with lambda expression. But compiler says "error: no matching function for call to .."
c++ c++11 lambda
add a comment |
I am trying to use this lambda expression in my code. And looks like something's wrong with definition of functor or lambda.
I believe the lambda expression is true. but I can't satisfy the prototype of function I defined.
Function definition:
template<typename E, typename container>
void for_each(Iterator<E,container> begin, std::function<void(E&)> fun)
{
do // does the given unary functor to elements
{ //from starting iterator 'til the end.
fun(*begin);
begin = begin.next();
}while(begin.hasNext());
fun(*begin);
}
And caller:
for_each(c.iterator(), [&](E& e){add(e);});
I except achieve this function call with lambda expression. But compiler says "error: no matching function for call to .."
c++ c++11 lambda
I am trying to use this lambda expression in my code. And looks like something's wrong with definition of functor or lambda.
I believe the lambda expression is true. but I can't satisfy the prototype of function I defined.
Function definition:
template<typename E, typename container>
void for_each(Iterator<E,container> begin, std::function<void(E&)> fun)
{
do // does the given unary functor to elements
{ //from starting iterator 'til the end.
fun(*begin);
begin = begin.next();
}while(begin.hasNext());
fun(*begin);
}
And caller:
for_each(c.iterator(), [&](E& e){add(e);});
I except achieve this function call with lambda expression. But compiler says "error: no matching function for call to .."
c++ c++11 lambda
c++ c++11 lambda
asked Dec 28 '18 at 23:16
drh0usedrh0use
225
225
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
lambda is not std::function, so deduction fails.
You might simply pass generic Functor:
template<typename E, typename container, typename F>
void for_each(Iterator<E, container> begin, F&& fun)
{
// ...
}
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Issue is with deduction, you might call it successfullyfor_each<E>(c.iterator(), [&](E& e){add(e);});with your version. lambda can convert tostd::function, but it is unrelated type.
– Jarod42
Dec 28 '18 at 23:28
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's whatstd::ref()is for.
– Deduplicator
Dec 29 '18 at 16:22
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for examplestd::invoke,std::thread, ... (which still might usestd::reference_wrapper).
– Jarod42
Dec 29 '18 at 17:01
add a comment |
Issues with the code and Possible solution approaches below:
1. A Lambda expression is not std::function, it can only be stored in an object of type std::function.
Approach: (With your existing signature):
std::function<void()> f = () -> void { // do something };
for_each(vec.begin(), vec.end(), f);
2. Better solution would be to use a Callable as a parameter (Go to end of the answer).
3. If you want to pull up your own version of for_each, why not follow the implementation that is consistent with other algorithms or for that matter with the implementation of for_each in the algorithm header.
Does all you want and is also consistent with other standard algorithms: en.cppreference.com/algorithms/for_each:
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
for (; first != last; ++first) {
f(*first);
}
return f;
}
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless youforwardthe reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argumentfor_each(x, y, (...){...});, which is not a good thing...
– HolyBlackCat
Dec 29 '18 at 16:15
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965308%2fno-matching-function-call-error-with-usage-of-lambda-expressions-and-stdfunct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
lambda is not std::function, so deduction fails.
You might simply pass generic Functor:
template<typename E, typename container, typename F>
void for_each(Iterator<E, container> begin, F&& fun)
{
// ...
}
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Issue is with deduction, you might call it successfullyfor_each<E>(c.iterator(), [&](E& e){add(e);});with your version. lambda can convert tostd::function, but it is unrelated type.
– Jarod42
Dec 28 '18 at 23:28
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's whatstd::ref()is for.
– Deduplicator
Dec 29 '18 at 16:22
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for examplestd::invoke,std::thread, ... (which still might usestd::reference_wrapper).
– Jarod42
Dec 29 '18 at 17:01
add a comment |
lambda is not std::function, so deduction fails.
You might simply pass generic Functor:
template<typename E, typename container, typename F>
void for_each(Iterator<E, container> begin, F&& fun)
{
// ...
}
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Issue is with deduction, you might call it successfullyfor_each<E>(c.iterator(), [&](E& e){add(e);});with your version. lambda can convert tostd::function, but it is unrelated type.
– Jarod42
Dec 28 '18 at 23:28
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's whatstd::ref()is for.
– Deduplicator
Dec 29 '18 at 16:22
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for examplestd::invoke,std::thread, ... (which still might usestd::reference_wrapper).
– Jarod42
Dec 29 '18 at 17:01
add a comment |
lambda is not std::function, so deduction fails.
You might simply pass generic Functor:
template<typename E, typename container, typename F>
void for_each(Iterator<E, container> begin, F&& fun)
{
// ...
}
lambda is not std::function, so deduction fails.
You might simply pass generic Functor:
template<typename E, typename container, typename F>
void for_each(Iterator<E, container> begin, F&& fun)
{
// ...
}
answered Dec 28 '18 at 23:18
Jarod42Jarod42
114k12101182
114k12101182
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Issue is with deduction, you might call it successfullyfor_each<E>(c.iterator(), [&](E& e){add(e);});with your version. lambda can convert tostd::function, but it is unrelated type.
– Jarod42
Dec 28 '18 at 23:28
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's whatstd::ref()is for.
– Deduplicator
Dec 29 '18 at 16:22
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for examplestd::invoke,std::thread, ... (which still might usestd::reference_wrapper).
– Jarod42
Dec 29 '18 at 17:01
add a comment |
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Issue is with deduction, you might call it successfullyfor_each<E>(c.iterator(), [&](E& e){add(e);});with your version. lambda can convert tostd::function, but it is unrelated type.
– Jarod42
Dec 28 '18 at 23:28
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's whatstd::ref()is for.
– Deduplicator
Dec 29 '18 at 16:22
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for examplestd::invoke,std::thread, ... (which still might usestd::reference_wrapper).
– Jarod42
Dec 29 '18 at 17:01
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Ty, it worked. But I was trying to imitate this link example. What part did I miss?
– drh0use
Dec 28 '18 at 23:23
Issue is with deduction, you might call it successfully
for_each<E>(c.iterator(), [&](E& e){add(e);}); with your version. lambda can convert to std::function, but it is unrelated type.– Jarod42
Dec 28 '18 at 23:28
Issue is with deduction, you might call it successfully
for_each<E>(c.iterator(), [&](E& e){add(e);}); with your version. lambda can convert to std::function, but it is unrelated type.– Jarod42
Dec 28 '18 at 23:28
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's what
std::ref() is for.– Deduplicator
Dec 29 '18 at 16:22
A curious decision to pass the function-object by reference instead of by value, like the standard-library does. If indirection is actually wanted, that's what
std::ref() is for.– Deduplicator
Dec 29 '18 at 16:22
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for example
std::invoke, std::thread, ... (which still might use std::reference_wrapper).– Jarod42
Dec 29 '18 at 17:01
@Deduplicator: pre-c++11, there are no forward references. now interfaces use them, for example
std::invoke, std::thread, ... (which still might use std::reference_wrapper).– Jarod42
Dec 29 '18 at 17:01
add a comment |
Issues with the code and Possible solution approaches below:
1. A Lambda expression is not std::function, it can only be stored in an object of type std::function.
Approach: (With your existing signature):
std::function<void()> f = () -> void { // do something };
for_each(vec.begin(), vec.end(), f);
2. Better solution would be to use a Callable as a parameter (Go to end of the answer).
3. If you want to pull up your own version of for_each, why not follow the implementation that is consistent with other algorithms or for that matter with the implementation of for_each in the algorithm header.
Does all you want and is also consistent with other standard algorithms: en.cppreference.com/algorithms/for_each:
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
for (; first != last; ++first) {
f(*first);
}
return f;
}
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless youforwardthe reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argumentfor_each(x, y, (...){...});, which is not a good thing...
– HolyBlackCat
Dec 29 '18 at 16:15
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
add a comment |
Issues with the code and Possible solution approaches below:
1. A Lambda expression is not std::function, it can only be stored in an object of type std::function.
Approach: (With your existing signature):
std::function<void()> f = () -> void { // do something };
for_each(vec.begin(), vec.end(), f);
2. Better solution would be to use a Callable as a parameter (Go to end of the answer).
3. If you want to pull up your own version of for_each, why not follow the implementation that is consistent with other algorithms or for that matter with the implementation of for_each in the algorithm header.
Does all you want and is also consistent with other standard algorithms: en.cppreference.com/algorithms/for_each:
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
for (; first != last; ++first) {
f(*first);
}
return f;
}
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless youforwardthe reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argumentfor_each(x, y, (...){...});, which is not a good thing...
– HolyBlackCat
Dec 29 '18 at 16:15
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
add a comment |
Issues with the code and Possible solution approaches below:
1. A Lambda expression is not std::function, it can only be stored in an object of type std::function.
Approach: (With your existing signature):
std::function<void()> f = () -> void { // do something };
for_each(vec.begin(), vec.end(), f);
2. Better solution would be to use a Callable as a parameter (Go to end of the answer).
3. If you want to pull up your own version of for_each, why not follow the implementation that is consistent with other algorithms or for that matter with the implementation of for_each in the algorithm header.
Does all you want and is also consistent with other standard algorithms: en.cppreference.com/algorithms/for_each:
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
for (; first != last; ++first) {
f(*first);
}
return f;
}
Issues with the code and Possible solution approaches below:
1. A Lambda expression is not std::function, it can only be stored in an object of type std::function.
Approach: (With your existing signature):
std::function<void()> f = () -> void { // do something };
for_each(vec.begin(), vec.end(), f);
2. Better solution would be to use a Callable as a parameter (Go to end of the answer).
3. If you want to pull up your own version of for_each, why not follow the implementation that is consistent with other algorithms or for that matter with the implementation of for_each in the algorithm header.
Does all you want and is also consistent with other standard algorithms: en.cppreference.com/algorithms/for_each:
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
{
for (; first != last; ++first) {
f(*first);
}
return f;
}
edited Dec 29 '18 at 16:39
answered Dec 29 '18 at 16:07
OptimatonOptimaton
3815
3815
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless youforwardthe reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argumentfor_each(x, y, (...){...});, which is not a good thing...
– HolyBlackCat
Dec 29 '18 at 16:15
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
add a comment |
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless youforwardthe reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argumentfor_each(x, y, (...){...});, which is not a good thing...
– HolyBlackCat
Dec 29 '18 at 16:15
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless you
forward the reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argument for_each(x, y, (...){...});, which is not a good thing...– HolyBlackCat
Dec 29 '18 at 16:15
Using a forwarding reference isn't going to prevent you from being "able to reuse the function object" unless you
forward the reference. And using an lvalue reference instead prevents you from calling your function with a lambda as an argument for_each(x, y, (...){...});, which is not a good thing...– HolyBlackCat
Dec 29 '18 at 16:15
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
@HolyBlackCat Incorporated the suggested changes. Must have sneaked in as I had to type it in due to some issues while copying. :p
– Optimaton
Dec 29 '18 at 16:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965308%2fno-matching-function-call-error-with-usage-of-lambda-expressions-and-stdfunct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown