Function Objects Set in C++
So basically I want to create a set of Function objects.
In python if we do:
def func():
print "a"
a = func
b = func
fset = set()
fset.insert(a)
fset.insert(b)
In this case fset will have only one function since both a and b are same in python.
But in C++, if I create function objects for same function both a and b will be two different objects of a set. Is there any way that two objects of same function be same?
In C++:
void func(){
cout << "a";
}
function<void()> a = bind(func);
function<void()> b = bind(func);
Now I want if a or its pointer is already present in the set, b should not be added.
c++
|
show 4 more comments
So basically I want to create a set of Function objects.
In python if we do:
def func():
print "a"
a = func
b = func
fset = set()
fset.insert(a)
fset.insert(b)
In this case fset will have only one function since both a and b are same in python.
But in C++, if I create function objects for same function both a and b will be two different objects of a set. Is there any way that two objects of same function be same?
In C++:
void func(){
cout << "a";
}
function<void()> a = bind(func);
function<void()> b = bind(func);
Now I want if a or its pointer is already present in the set, b should not be added.
c++
1
a
andb
are essentially two pointers to the same object, not two instances of the same class. You can havestd::set<std::function<void()>*>
, or possibly a smart pointer - it'll behave in a similar manner.
– Igor Tandetnik
Nov 23 '18 at 5:22
But in this case also every time creating a pointer of function<void()> a = function(); function<void()> b = function(); Both a and b will still have different pointers right?
– 250
Nov 23 '18 at 5:23
2
A set, by definition, contains values distinct from one another. It's not clear what you're talking about here, when you suggest that you are putting the "same function" into a set as two different objects. Perhaps you should back up your question with some C++ code to illustrate your problem.
– paddy
Nov 23 '18 at 5:25
You say "creating a pointer", but your code doesn't actually create any pointers. It's also not clear what you mean byfunction()
- that doesn't appear syntactically valid and likely won't compile.
– Igor Tandetnik
Nov 23 '18 at 5:25
@IgorTandetnik they are talking about your comment which storesstd::function<void()>*
– paddy
Nov 23 '18 at 5:26
|
show 4 more comments
So basically I want to create a set of Function objects.
In python if we do:
def func():
print "a"
a = func
b = func
fset = set()
fset.insert(a)
fset.insert(b)
In this case fset will have only one function since both a and b are same in python.
But in C++, if I create function objects for same function both a and b will be two different objects of a set. Is there any way that two objects of same function be same?
In C++:
void func(){
cout << "a";
}
function<void()> a = bind(func);
function<void()> b = bind(func);
Now I want if a or its pointer is already present in the set, b should not be added.
c++
So basically I want to create a set of Function objects.
In python if we do:
def func():
print "a"
a = func
b = func
fset = set()
fset.insert(a)
fset.insert(b)
In this case fset will have only one function since both a and b are same in python.
But in C++, if I create function objects for same function both a and b will be two different objects of a set. Is there any way that two objects of same function be same?
In C++:
void func(){
cout << "a";
}
function<void()> a = bind(func);
function<void()> b = bind(func);
Now I want if a or its pointer is already present in the set, b should not be added.
c++
c++
edited Nov 23 '18 at 5:32
250
asked Nov 23 '18 at 5:20
250250
216214
216214
1
a
andb
are essentially two pointers to the same object, not two instances of the same class. You can havestd::set<std::function<void()>*>
, or possibly a smart pointer - it'll behave in a similar manner.
– Igor Tandetnik
Nov 23 '18 at 5:22
But in this case also every time creating a pointer of function<void()> a = function(); function<void()> b = function(); Both a and b will still have different pointers right?
– 250
Nov 23 '18 at 5:23
2
A set, by definition, contains values distinct from one another. It's not clear what you're talking about here, when you suggest that you are putting the "same function" into a set as two different objects. Perhaps you should back up your question with some C++ code to illustrate your problem.
– paddy
Nov 23 '18 at 5:25
You say "creating a pointer", but your code doesn't actually create any pointers. It's also not clear what you mean byfunction()
- that doesn't appear syntactically valid and likely won't compile.
– Igor Tandetnik
Nov 23 '18 at 5:25
@IgorTandetnik they are talking about your comment which storesstd::function<void()>*
– paddy
Nov 23 '18 at 5:26
|
show 4 more comments
1
a
andb
are essentially two pointers to the same object, not two instances of the same class. You can havestd::set<std::function<void()>*>
, or possibly a smart pointer - it'll behave in a similar manner.
– Igor Tandetnik
Nov 23 '18 at 5:22
But in this case also every time creating a pointer of function<void()> a = function(); function<void()> b = function(); Both a and b will still have different pointers right?
– 250
Nov 23 '18 at 5:23
2
A set, by definition, contains values distinct from one another. It's not clear what you're talking about here, when you suggest that you are putting the "same function" into a set as two different objects. Perhaps you should back up your question with some C++ code to illustrate your problem.
– paddy
Nov 23 '18 at 5:25
You say "creating a pointer", but your code doesn't actually create any pointers. It's also not clear what you mean byfunction()
- that doesn't appear syntactically valid and likely won't compile.
– Igor Tandetnik
Nov 23 '18 at 5:25
@IgorTandetnik they are talking about your comment which storesstd::function<void()>*
– paddy
Nov 23 '18 at 5:26
1
1
a
and b
are essentially two pointers to the same object, not two instances of the same class. You can have std::set<std::function<void()>*>
, or possibly a smart pointer - it'll behave in a similar manner.– Igor Tandetnik
Nov 23 '18 at 5:22
a
and b
are essentially two pointers to the same object, not two instances of the same class. You can have std::set<std::function<void()>*>
, or possibly a smart pointer - it'll behave in a similar manner.– Igor Tandetnik
Nov 23 '18 at 5:22
But in this case also every time creating a pointer of function<void()> a = function(); function<void()> b = function(); Both a and b will still have different pointers right?
– 250
Nov 23 '18 at 5:23
But in this case also every time creating a pointer of function<void()> a = function(); function<void()> b = function(); Both a and b will still have different pointers right?
– 250
Nov 23 '18 at 5:23
2
2
A set, by definition, contains values distinct from one another. It's not clear what you're talking about here, when you suggest that you are putting the "same function" into a set as two different objects. Perhaps you should back up your question with some C++ code to illustrate your problem.
– paddy
Nov 23 '18 at 5:25
A set, by definition, contains values distinct from one another. It's not clear what you're talking about here, when you suggest that you are putting the "same function" into a set as two different objects. Perhaps you should back up your question with some C++ code to illustrate your problem.
– paddy
Nov 23 '18 at 5:25
You say "creating a pointer", but your code doesn't actually create any pointers. It's also not clear what you mean by
function()
- that doesn't appear syntactically valid and likely won't compile.– Igor Tandetnik
Nov 23 '18 at 5:25
You say "creating a pointer", but your code doesn't actually create any pointers. It's also not clear what you mean by
function()
- that doesn't appear syntactically valid and likely won't compile.– Igor Tandetnik
Nov 23 '18 at 5:25
@IgorTandetnik they are talking about your comment which stores
std::function<void()>*
– paddy
Nov 23 '18 at 5:26
@IgorTandetnik they are talking about your comment which stores
std::function<void()>*
– paddy
Nov 23 '18 at 5:26
|
show 4 more comments
1 Answer
1
active
oldest
votes
If you have only void functions(or all functions have the same signature), use simply C type function pointers as std::sets
's template type.
This will work and as a plus no type erasure overheads of std::function
.
void func() {}
void func2() {}
using fPtrType = void(*)(); // convenience type
int main()
{
std::set<fPtrType> fset;
fPtrType a = func;
fPtrType b = func;
fset.emplace(a);
fset.emplace(b);
fset.emplace(func2);
std::cout << fset.size(); // prints 2
return 0;
}
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
1
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6: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%2f53440997%2ffunction-objects-set-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have only void functions(or all functions have the same signature), use simply C type function pointers as std::sets
's template type.
This will work and as a plus no type erasure overheads of std::function
.
void func() {}
void func2() {}
using fPtrType = void(*)(); // convenience type
int main()
{
std::set<fPtrType> fset;
fPtrType a = func;
fPtrType b = func;
fset.emplace(a);
fset.emplace(b);
fset.emplace(func2);
std::cout << fset.size(); // prints 2
return 0;
}
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
1
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6:21
add a comment |
If you have only void functions(or all functions have the same signature), use simply C type function pointers as std::sets
's template type.
This will work and as a plus no type erasure overheads of std::function
.
void func() {}
void func2() {}
using fPtrType = void(*)(); // convenience type
int main()
{
std::set<fPtrType> fset;
fPtrType a = func;
fPtrType b = func;
fset.emplace(a);
fset.emplace(b);
fset.emplace(func2);
std::cout << fset.size(); // prints 2
return 0;
}
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
1
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6:21
add a comment |
If you have only void functions(or all functions have the same signature), use simply C type function pointers as std::sets
's template type.
This will work and as a plus no type erasure overheads of std::function
.
void func() {}
void func2() {}
using fPtrType = void(*)(); // convenience type
int main()
{
std::set<fPtrType> fset;
fPtrType a = func;
fPtrType b = func;
fset.emplace(a);
fset.emplace(b);
fset.emplace(func2);
std::cout << fset.size(); // prints 2
return 0;
}
If you have only void functions(or all functions have the same signature), use simply C type function pointers as std::sets
's template type.
This will work and as a plus no type erasure overheads of std::function
.
void func() {}
void func2() {}
using fPtrType = void(*)(); // convenience type
int main()
{
std::set<fPtrType> fset;
fPtrType a = func;
fPtrType b = func;
fset.emplace(a);
fset.emplace(b);
fset.emplace(func2);
std::cout << fset.size(); // prints 2
return 0;
}
answered Nov 23 '18 at 5:46
JeJoJeJo
4,5173826
4,5173826
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
1
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6:21
add a comment |
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
1
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6:21
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
Will this also work if both a and b have a different scope?
– 250
Nov 23 '18 at 5:59
1
1
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6:21
@250 Did you mean this.
– JeJo
Nov 23 '18 at 6: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%2f53440997%2ffunction-objects-set-in-c%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
1
a
andb
are essentially two pointers to the same object, not two instances of the same class. You can havestd::set<std::function<void()>*>
, or possibly a smart pointer - it'll behave in a similar manner.– Igor Tandetnik
Nov 23 '18 at 5:22
But in this case also every time creating a pointer of function<void()> a = function(); function<void()> b = function(); Both a and b will still have different pointers right?
– 250
Nov 23 '18 at 5:23
2
A set, by definition, contains values distinct from one another. It's not clear what you're talking about here, when you suggest that you are putting the "same function" into a set as two different objects. Perhaps you should back up your question with some C++ code to illustrate your problem.
– paddy
Nov 23 '18 at 5:25
You say "creating a pointer", but your code doesn't actually create any pointers. It's also not clear what you mean by
function()
- that doesn't appear syntactically valid and likely won't compile.– Igor Tandetnik
Nov 23 '18 at 5:25
@IgorTandetnik they are talking about your comment which stores
std::function<void()>*
– paddy
Nov 23 '18 at 5:26