Detecting existence of a class memberStatic class member destruction in C++Elementwise perfect forwarding of a member rangeDetecting cyclesClass member method based multithreading [C++]Small coroutine classUsing shared_ptr as class memberCallback class to callback member (virtual) functionsAn iterator wrapper that on dereferencing, returns the value of a member (function)product member of CSC matrix classC++ Class for optionally constructing arbitrary derived classes
If you inherit a Roth 401(k), is it taxed?
What language is Raven using for her attack in the new 52?
Why is it considered acid rain with pH <5.6?
Why does aggregate initialization not work anymore since C++20 if a constructor is explicitly defaulted or deleted?
Surviving a planet collision?
Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?
A variant of the Multiple Traveling Salesman Problem
Is SecureRandom.ints() secure?
Can this party play the Lost Mine of Phandelver adventure without too much trouble?
A cubeful of three-dimensional devilry
Shouldn't there be "us" instead of "our" in this sentence?
What force enables us to walk? Friction or normal reaction?
Why force the nose of 737 Max down in the first place?
How can Paypal know my card is being used in another account?
Why is it "on the inside" and not "in the inside"?
Is it safe if the neutral lead is exposed and disconnected?
GNU sort stable sort when sort does not know sort order
Is there a word to describe someone who is, or the state of being, content with hanging around others without interacting with them?
Convert graph format for Mathematica graph functions
Can Lightning Lure be used to knock out a creature like a magical Taser?
Piece of chess engine, which accomplishes move generation
Was Donald Trump at ground zero helping out on 9-11?
Why does the Eurostar not show youth pricing?
Why did Windows 95 crash the whole system but newer Windows only crashed programs?
Detecting existence of a class member
Static class member destruction in C++Elementwise perfect forwarding of a member rangeDetecting cyclesClass member method based multithreading [C++]Small coroutine classUsing shared_ptr as class memberCallback class to callback member (virtual) functionsAn iterator wrapper that on dereferencing, returns the value of a member (function)product member of CSC matrix classC++ Class for optionally constructing arbitrary derived classes
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I wanted to detect if I have a member in a simple POD struct and after some searching and merging some methods I found on the web I've come up with this solution:
#include <iostream>
template<class...Fs>
struct funcs_t;
template<class F0, class...Fs>
struct funcs_t<F0, Fs...>: F0, funcs_t<Fs...>
funcs_t(F0 f0, Fs... fs):
F0(std::move(f0)), funcs_t<Fs...>(std::move(fs)...)
using F0::operator();
using funcs_t<Fs...>::operator();
;
template<class F>
struct funcs_t<F>:F
funcs_t(F f) : F(std::move(f)) ;
using F::operator();
;
template<class...Fs>
funcs_t<std::decay_t<Fs>...> funcs(Fs&&...fs)
return std::forward<Fs>(fs)...;
#define HAS_MEMBER(cls, memb)
[]()
auto with_memb = [](auto &&A, int x)
-> decltype(typename std::remove_reference<decltype(A)>::type().memb, std::true_type())
return std::true_type();
;
auto with_no_memb = [](auto &&A, float x) -> std::false_type
return std::false_type();
;
auto has_memb = funcs(with_memb, with_no_memb);
return has_memb(cls(), 1);
()
struct A
int a;
int b;
;
int main(int argc, char const *argv[])
std::cout << HAS_MEMBER(A, b) << std::endl;
return 0;
c++ c++14 meta-programming
$endgroup$
add a comment |
$begingroup$
I wanted to detect if I have a member in a simple POD struct and after some searching and merging some methods I found on the web I've come up with this solution:
#include <iostream>
template<class...Fs>
struct funcs_t;
template<class F0, class...Fs>
struct funcs_t<F0, Fs...>: F0, funcs_t<Fs...>
funcs_t(F0 f0, Fs... fs):
F0(std::move(f0)), funcs_t<Fs...>(std::move(fs)...)
using F0::operator();
using funcs_t<Fs...>::operator();
;
template<class F>
struct funcs_t<F>:F
funcs_t(F f) : F(std::move(f)) ;
using F::operator();
;
template<class...Fs>
funcs_t<std::decay_t<Fs>...> funcs(Fs&&...fs)
return std::forward<Fs>(fs)...;
#define HAS_MEMBER(cls, memb)
[]()
auto with_memb = [](auto &&A, int x)
-> decltype(typename std::remove_reference<decltype(A)>::type().memb, std::true_type())
return std::true_type();
;
auto with_no_memb = [](auto &&A, float x) -> std::false_type
return std::false_type();
;
auto has_memb = funcs(with_memb, with_no_memb);
return has_memb(cls(), 1);
()
struct A
int a;
int b;
;
int main(int argc, char const *argv[])
std::cout << HAS_MEMBER(A, b) << std::endl;
return 0;
c++ c++14 meta-programming
$endgroup$
1
$begingroup$
Why do you need this? What are you trying to achieve?
$endgroup$
– Martin York
Jul 19 at 16:39
$begingroup$
I have different pod structs with many common fields with the same meaning and I wanted to print them on screen. I will see if I can use it for other things but for now that's what I needed it for.
$endgroup$
– Pangi
Jul 19 at 18:45
$begingroup$
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. If you want a fixed-version of your code to be available, post it as answer linking to Deduplicator's for credit (or post it as a new question, but it's hardly a major change).
$endgroup$
– Mast
Jul 19 at 19:04
$begingroup$
Ok, I'll keep that in mind.
$endgroup$
– Pangi
Jul 19 at 19:16
add a comment |
$begingroup$
I wanted to detect if I have a member in a simple POD struct and after some searching and merging some methods I found on the web I've come up with this solution:
#include <iostream>
template<class...Fs>
struct funcs_t;
template<class F0, class...Fs>
struct funcs_t<F0, Fs...>: F0, funcs_t<Fs...>
funcs_t(F0 f0, Fs... fs):
F0(std::move(f0)), funcs_t<Fs...>(std::move(fs)...)
using F0::operator();
using funcs_t<Fs...>::operator();
;
template<class F>
struct funcs_t<F>:F
funcs_t(F f) : F(std::move(f)) ;
using F::operator();
;
template<class...Fs>
funcs_t<std::decay_t<Fs>...> funcs(Fs&&...fs)
return std::forward<Fs>(fs)...;
#define HAS_MEMBER(cls, memb)
[]()
auto with_memb = [](auto &&A, int x)
-> decltype(typename std::remove_reference<decltype(A)>::type().memb, std::true_type())
return std::true_type();
;
auto with_no_memb = [](auto &&A, float x) -> std::false_type
return std::false_type();
;
auto has_memb = funcs(with_memb, with_no_memb);
return has_memb(cls(), 1);
()
struct A
int a;
int b;
;
int main(int argc, char const *argv[])
std::cout << HAS_MEMBER(A, b) << std::endl;
return 0;
c++ c++14 meta-programming
$endgroup$
I wanted to detect if I have a member in a simple POD struct and after some searching and merging some methods I found on the web I've come up with this solution:
#include <iostream>
template<class...Fs>
struct funcs_t;
template<class F0, class...Fs>
struct funcs_t<F0, Fs...>: F0, funcs_t<Fs...>
funcs_t(F0 f0, Fs... fs):
F0(std::move(f0)), funcs_t<Fs...>(std::move(fs)...)
using F0::operator();
using funcs_t<Fs...>::operator();
;
template<class F>
struct funcs_t<F>:F
funcs_t(F f) : F(std::move(f)) ;
using F::operator();
;
template<class...Fs>
funcs_t<std::decay_t<Fs>...> funcs(Fs&&...fs)
return std::forward<Fs>(fs)...;
#define HAS_MEMBER(cls, memb)
[]()
auto with_memb = [](auto &&A, int x)
-> decltype(typename std::remove_reference<decltype(A)>::type().memb, std::true_type())
return std::true_type();
;
auto with_no_memb = [](auto &&A, float x) -> std::false_type
return std::false_type();
;
auto has_memb = funcs(with_memb, with_no_memb);
return has_memb(cls(), 1);
()
struct A
int a;
int b;
;
int main(int argc, char const *argv[])
std::cout << HAS_MEMBER(A, b) << std::endl;
return 0;
c++ c++14 meta-programming
c++ c++14 meta-programming
edited Jul 19 at 19:04
Mast
7,8607 gold badges37 silver badges91 bronze badges
7,8607 gold badges37 silver badges91 bronze badges
asked Jul 19 at 8:26
PangiPangi
604 bronze badges
604 bronze badges
1
$begingroup$
Why do you need this? What are you trying to achieve?
$endgroup$
– Martin York
Jul 19 at 16:39
$begingroup$
I have different pod structs with many common fields with the same meaning and I wanted to print them on screen. I will see if I can use it for other things but for now that's what I needed it for.
$endgroup$
– Pangi
Jul 19 at 18:45
$begingroup$
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. If you want a fixed-version of your code to be available, post it as answer linking to Deduplicator's for credit (or post it as a new question, but it's hardly a major change).
$endgroup$
– Mast
Jul 19 at 19:04
$begingroup$
Ok, I'll keep that in mind.
$endgroup$
– Pangi
Jul 19 at 19:16
add a comment |
1
$begingroup$
Why do you need this? What are you trying to achieve?
$endgroup$
– Martin York
Jul 19 at 16:39
$begingroup$
I have different pod structs with many common fields with the same meaning and I wanted to print them on screen. I will see if I can use it for other things but for now that's what I needed it for.
$endgroup$
– Pangi
Jul 19 at 18:45
$begingroup$
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. If you want a fixed-version of your code to be available, post it as answer linking to Deduplicator's for credit (or post it as a new question, but it's hardly a major change).
$endgroup$
– Mast
Jul 19 at 19:04
$begingroup$
Ok, I'll keep that in mind.
$endgroup$
– Pangi
Jul 19 at 19:16
1
1
$begingroup$
Why do you need this? What are you trying to achieve?
$endgroup$
– Martin York
Jul 19 at 16:39
$begingroup$
Why do you need this? What are you trying to achieve?
$endgroup$
– Martin York
Jul 19 at 16:39
$begingroup$
I have different pod structs with many common fields with the same meaning and I wanted to print them on screen. I will see if I can use it for other things but for now that's what I needed it for.
$endgroup$
– Pangi
Jul 19 at 18:45
$begingroup$
I have different pod structs with many common fields with the same meaning and I wanted to print them on screen. I will see if I can use it for other things but for now that's what I needed it for.
$endgroup$
– Pangi
Jul 19 at 18:45
$begingroup$
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. If you want a fixed-version of your code to be available, post it as answer linking to Deduplicator's for credit (or post it as a new question, but it's hardly a major change).
$endgroup$
– Mast
Jul 19 at 19:04
$begingroup$
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. If you want a fixed-version of your code to be available, post it as answer linking to Deduplicator's for credit (or post it as a new question, but it's hardly a major change).
$endgroup$
– Mast
Jul 19 at 19:04
$begingroup$
Ok, I'll keep that in mind.
$endgroup$
– Pangi
Jul 19 at 19:16
$begingroup$
Ok, I'll keep that in mind.
$endgroup$
– Pangi
Jul 19 at 19:16
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Your detector fails in the face of overloading and templating. Also, it will only detect (static) member-variables and (static) member-functions. While you can extend it to types (and type-aliases), accepting templates and overloading would need better language-provided reflection-facilities.
Anyway, pure existence is generally uninteresting, supported operations count.
funcs()andfuncs_tare nearly a generally useful abstraction.Just use perfect forwarding instead of by-value and
std::move(). Allowing for all callables, includingfinalclasses, function-pointers, member-function-pointers, and the same wrapped in astd::reference_wrapperwould admittedly add significant amounts of code.A good name would be
overloaded.HAS_MEMBERneedlessly depends on default-constructing the passed class. Fix that by usingdecltype,std::declval()and unevaluated contexts.If you don't use an argument, don't name it. Specifically for
main(), just don't ask for it.Don't use
std::endl. In the rare cases you actually need to flush manually, be explicit and usestd::flush. Nearly always you are just crippling performance.return 0;is implicit formain(). Make of that what you will.
$endgroup$
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: "196"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f224463%2fdetecting-existence-of-a-class-member%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
$begingroup$
Your detector fails in the face of overloading and templating. Also, it will only detect (static) member-variables and (static) member-functions. While you can extend it to types (and type-aliases), accepting templates and overloading would need better language-provided reflection-facilities.
Anyway, pure existence is generally uninteresting, supported operations count.
funcs()andfuncs_tare nearly a generally useful abstraction.Just use perfect forwarding instead of by-value and
std::move(). Allowing for all callables, includingfinalclasses, function-pointers, member-function-pointers, and the same wrapped in astd::reference_wrapperwould admittedly add significant amounts of code.A good name would be
overloaded.HAS_MEMBERneedlessly depends on default-constructing the passed class. Fix that by usingdecltype,std::declval()and unevaluated contexts.If you don't use an argument, don't name it. Specifically for
main(), just don't ask for it.Don't use
std::endl. In the rare cases you actually need to flush manually, be explicit and usestd::flush. Nearly always you are just crippling performance.return 0;is implicit formain(). Make of that what you will.
$endgroup$
add a comment |
$begingroup$
Your detector fails in the face of overloading and templating. Also, it will only detect (static) member-variables and (static) member-functions. While you can extend it to types (and type-aliases), accepting templates and overloading would need better language-provided reflection-facilities.
Anyway, pure existence is generally uninteresting, supported operations count.
funcs()andfuncs_tare nearly a generally useful abstraction.Just use perfect forwarding instead of by-value and
std::move(). Allowing for all callables, includingfinalclasses, function-pointers, member-function-pointers, and the same wrapped in astd::reference_wrapperwould admittedly add significant amounts of code.A good name would be
overloaded.HAS_MEMBERneedlessly depends on default-constructing the passed class. Fix that by usingdecltype,std::declval()and unevaluated contexts.If you don't use an argument, don't name it. Specifically for
main(), just don't ask for it.Don't use
std::endl. In the rare cases you actually need to flush manually, be explicit and usestd::flush. Nearly always you are just crippling performance.return 0;is implicit formain(). Make of that what you will.
$endgroup$
add a comment |
$begingroup$
Your detector fails in the face of overloading and templating. Also, it will only detect (static) member-variables and (static) member-functions. While you can extend it to types (and type-aliases), accepting templates and overloading would need better language-provided reflection-facilities.
Anyway, pure existence is generally uninteresting, supported operations count.
funcs()andfuncs_tare nearly a generally useful abstraction.Just use perfect forwarding instead of by-value and
std::move(). Allowing for all callables, includingfinalclasses, function-pointers, member-function-pointers, and the same wrapped in astd::reference_wrapperwould admittedly add significant amounts of code.A good name would be
overloaded.HAS_MEMBERneedlessly depends on default-constructing the passed class. Fix that by usingdecltype,std::declval()and unevaluated contexts.If you don't use an argument, don't name it. Specifically for
main(), just don't ask for it.Don't use
std::endl. In the rare cases you actually need to flush manually, be explicit and usestd::flush. Nearly always you are just crippling performance.return 0;is implicit formain(). Make of that what you will.
$endgroup$
Your detector fails in the face of overloading and templating. Also, it will only detect (static) member-variables and (static) member-functions. While you can extend it to types (and type-aliases), accepting templates and overloading would need better language-provided reflection-facilities.
Anyway, pure existence is generally uninteresting, supported operations count.
funcs()andfuncs_tare nearly a generally useful abstraction.Just use perfect forwarding instead of by-value and
std::move(). Allowing for all callables, includingfinalclasses, function-pointers, member-function-pointers, and the same wrapped in astd::reference_wrapperwould admittedly add significant amounts of code.A good name would be
overloaded.HAS_MEMBERneedlessly depends on default-constructing the passed class. Fix that by usingdecltype,std::declval()and unevaluated contexts.If you don't use an argument, don't name it. Specifically for
main(), just don't ask for it.Don't use
std::endl. In the rare cases you actually need to flush manually, be explicit and usestd::flush. Nearly always you are just crippling performance.return 0;is implicit formain(). Make of that what you will.
edited Jul 19 at 16:54
answered Jul 19 at 9:18
DeduplicatorDeduplicator
13.3k20 silver badges55 bronze badges
13.3k20 silver badges55 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f224463%2fdetecting-existence-of-a-class-member%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
$begingroup$
Why do you need this? What are you trying to achieve?
$endgroup$
– Martin York
Jul 19 at 16:39
$begingroup$
I have different pod structs with many common fields with the same meaning and I wanted to print them on screen. I will see if I can use it for other things but for now that's what I needed it for.
$endgroup$
– Pangi
Jul 19 at 18:45
$begingroup$
Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. If you want a fixed-version of your code to be available, post it as answer linking to Deduplicator's for credit (or post it as a new question, but it's hardly a major change).
$endgroup$
– Mast
Jul 19 at 19:04
$begingroup$
Ok, I'll keep that in mind.
$endgroup$
– Pangi
Jul 19 at 19:16