Removing class pointer from member function pointer typeC++ function typesHow to call a parent class function from derived class function?error: request for member '..' in '..' which is of non-class typePretty-print C++ STL containersC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?What's the difference between std::move and std::forwardWhy is my program slow when looping over exactly 8192 elements?Partial template class specialization with member function pointerReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInstantiating a templated class from a templated member function of a not templated classInconsistent parameter pack deduction int and int& in variadic templated member function that creates a thread that runs a member function
Should I include salary information on my CV?
Should I tell my insurance company I'm making payments on my new car?
What are the benefits of using the X Card safety tool in comparison to plain communication?
MH370 blackbox - is it still possible to retrieve data from it?
A player is constantly pestering me about rules, what do I do as a DM?
What kind of wire should I use to pigtail an outlet?
How come I was asked by a CBP officer why I was in the US?
Is it damaging to turn off a small fridge for two days every week?
What happens when your group is victim of a surprise attack but you can't be surprised?
Do equal angles necessarily mean a polygon is regular?
How to determine what is the correct level of detail when modelling?
Why is Madam Hooch not a professor?
Can a Horncaller control a Druid who is using Wild Shape?
No IMPLICIT_CONVERSION warning in this query plan
Alphabet completion rate
Links to webpages in books
Distance Matrix (plugin) - QGIS
Change CPU MHz from Registry
Unusual mail headers, evidence of an attempted attack. Have I been pwned?
How well known and how commonly used was Huffman coding in 1979?
Story-based adventure with functions and relationships
Why is the voltage measurement of this circuit different when the switch is on?
Isn't this a trivial corollary?
Is there a maximum distance from a planet that a moon can orbit?
Removing class pointer from member function pointer type
C++ function typesHow to call a parent class function from derived class function?error: request for member '..' in '..' which is of non-class typePretty-print C++ STL containersC++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?What's the difference between std::move and std::forwardWhy is my program slow when looping over exactly 8192 elements?Partial template class specialization with member function pointerReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviationsInstantiating a templated class from a templated member function of a not templated classInconsistent parameter pack deduction int and int& in variadic templated member function that creates a thread that runs a member function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I saw this code snippet in the example on cppreference for std::is_function and I don't understand how it works. Could someone explain to me why U gets deduced as it does in PM_traits?
struct A
int fun() const&;
;
template<typename>
struct PM_traits ;
template<class T, class U>
struct PM_traits<U T::*>
using member_type = U;
;
int main()
using T = PM_traits<decltype(&A::fun)>::member_type; // T is int() const&
c++ c++11 templates
New contributor
|
show 1 more comment
I saw this code snippet in the example on cppreference for std::is_function and I don't understand how it works. Could someone explain to me why U gets deduced as it does in PM_traits?
struct A
int fun() const&;
;
template<typename>
struct PM_traits ;
template<class T, class U>
struct PM_traits<U T::*>
using member_type = U;
;
int main()
using T = PM_traits<decltype(&A::fun)>::member_type; // T is int() const&
c++ c++11 templates
New contributor
I've verified that this is the deduced type with g++.T
is in factint() const&
.
– willtunnels
8 hours ago
Well, it seems you are right. But I have a hard time understanding what aint() const&
can possibly mean outside the context of a member function.const
and the ref qualifier&
are meaningless (and forbidden) on a free function.
– François Andrieux
8 hours ago
@FrançoisAndrieux you can simply it, by making a simple member function, non-const. It would still be a function type.
– SergeyA
8 hours ago
1
@FrançoisAndrieux Yes, they are forbidden on free functions, but not on function types in general. Remember that it's legal to declare (not define) a member function using a typedef, for example. Live example
– Angew
8 hours ago
@Angew That's fascinating, thank you for the example.
– François Andrieux
8 hours ago
|
show 1 more comment
I saw this code snippet in the example on cppreference for std::is_function and I don't understand how it works. Could someone explain to me why U gets deduced as it does in PM_traits?
struct A
int fun() const&;
;
template<typename>
struct PM_traits ;
template<class T, class U>
struct PM_traits<U T::*>
using member_type = U;
;
int main()
using T = PM_traits<decltype(&A::fun)>::member_type; // T is int() const&
c++ c++11 templates
New contributor
I saw this code snippet in the example on cppreference for std::is_function and I don't understand how it works. Could someone explain to me why U gets deduced as it does in PM_traits?
struct A
int fun() const&;
;
template<typename>
struct PM_traits ;
template<class T, class U>
struct PM_traits<U T::*>
using member_type = U;
;
int main()
using T = PM_traits<decltype(&A::fun)>::member_type; // T is int() const&
c++ c++11 templates
c++ c++11 templates
New contributor
New contributor
New contributor
asked 8 hours ago
willtunnelswilltunnels
312 bronze badges
312 bronze badges
New contributor
New contributor
I've verified that this is the deduced type with g++.T
is in factint() const&
.
– willtunnels
8 hours ago
Well, it seems you are right. But I have a hard time understanding what aint() const&
can possibly mean outside the context of a member function.const
and the ref qualifier&
are meaningless (and forbidden) on a free function.
– François Andrieux
8 hours ago
@FrançoisAndrieux you can simply it, by making a simple member function, non-const. It would still be a function type.
– SergeyA
8 hours ago
1
@FrançoisAndrieux Yes, they are forbidden on free functions, but not on function types in general. Remember that it's legal to declare (not define) a member function using a typedef, for example. Live example
– Angew
8 hours ago
@Angew That's fascinating, thank you for the example.
– François Andrieux
8 hours ago
|
show 1 more comment
I've verified that this is the deduced type with g++.T
is in factint() const&
.
– willtunnels
8 hours ago
Well, it seems you are right. But I have a hard time understanding what aint() const&
can possibly mean outside the context of a member function.const
and the ref qualifier&
are meaningless (and forbidden) on a free function.
– François Andrieux
8 hours ago
@FrançoisAndrieux you can simply it, by making a simple member function, non-const. It would still be a function type.
– SergeyA
8 hours ago
1
@FrançoisAndrieux Yes, they are forbidden on free functions, but not on function types in general. Remember that it's legal to declare (not define) a member function using a typedef, for example. Live example
– Angew
8 hours ago
@Angew That's fascinating, thank you for the example.
– François Andrieux
8 hours ago
I've verified that this is the deduced type with g++.
T
is in fact int() const&
.– willtunnels
8 hours ago
I've verified that this is the deduced type with g++.
T
is in fact int() const&
.– willtunnels
8 hours ago
Well, it seems you are right. But I have a hard time understanding what a
int() const&
can possibly mean outside the context of a member function. const
and the ref qualifier &
are meaningless (and forbidden) on a free function.– François Andrieux
8 hours ago
Well, it seems you are right. But I have a hard time understanding what a
int() const&
can possibly mean outside the context of a member function. const
and the ref qualifier &
are meaningless (and forbidden) on a free function.– François Andrieux
8 hours ago
@FrançoisAndrieux you can simply it, by making a simple member function, non-const. It would still be a function type.
– SergeyA
8 hours ago
@FrançoisAndrieux you can simply it, by making a simple member function, non-const. It would still be a function type.
– SergeyA
8 hours ago
1
1
@FrançoisAndrieux Yes, they are forbidden on free functions, but not on function types in general. Remember that it's legal to declare (not define) a member function using a typedef, for example. Live example
– Angew
8 hours ago
@FrançoisAndrieux Yes, they are forbidden on free functions, but not on function types in general. Remember that it's legal to declare (not define) a member function using a typedef, for example. Live example
– Angew
8 hours ago
@Angew That's fascinating, thank you for the example.
– François Andrieux
8 hours ago
@Angew That's fascinating, thank you for the example.
– François Andrieux
8 hours ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
U T::*
is a type such that when we have U T::* p
, p
points to a member of class T
, and that member is of type U
.
fun
is a function of type int () const &
: a const &
-qualified function taking no parameters and returning int
, and it's a member of class A
. Therefore, in the deduction, T
is deduced to A
and U
is deduced to the type of A::fun
, which is int () const &
.
It may look a bit confusing, because if the type of &A::fun
was spelled out explicitly, it would have to be written int (A::*)() const &
. However, in the template's case, the type int () const &
is "hidden" behind the name U
, so the pointer to member is then just U A::*
. It's similar to how type names can be used to simplify the syntax of normal function pointers:
int foo(char, double) return 42;
using Fun = int (char, double);
Fun *p = &foo;
// instead of:
int (*q)(char, double) = &foo;
The same happens in the template, just with A::*
instead of *
.
I can writeint(A::* p)() const & = &A::fun;
butint () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?
– willtunnels
8 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can writeusing V = int () const &; V A::* p = &A::fun;
. It's the same as writingconst int *p;
vs.using T = int*; const T p;
.
– Angew
7 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat outcannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)
– willtunnels
6 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
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
);
);
willtunnels is a new contributor. Be nice, and check out our Code of Conduct.
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%2f56709483%2fremoving-class-pointer-from-member-function-pointer-type%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
U T::*
is a type such that when we have U T::* p
, p
points to a member of class T
, and that member is of type U
.
fun
is a function of type int () const &
: a const &
-qualified function taking no parameters and returning int
, and it's a member of class A
. Therefore, in the deduction, T
is deduced to A
and U
is deduced to the type of A::fun
, which is int () const &
.
It may look a bit confusing, because if the type of &A::fun
was spelled out explicitly, it would have to be written int (A::*)() const &
. However, in the template's case, the type int () const &
is "hidden" behind the name U
, so the pointer to member is then just U A::*
. It's similar to how type names can be used to simplify the syntax of normal function pointers:
int foo(char, double) return 42;
using Fun = int (char, double);
Fun *p = &foo;
// instead of:
int (*q)(char, double) = &foo;
The same happens in the template, just with A::*
instead of *
.
I can writeint(A::* p)() const & = &A::fun;
butint () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?
– willtunnels
8 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can writeusing V = int () const &; V A::* p = &A::fun;
. It's the same as writingconst int *p;
vs.using T = int*; const T p;
.
– Angew
7 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat outcannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)
– willtunnels
6 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
add a comment |
U T::*
is a type such that when we have U T::* p
, p
points to a member of class T
, and that member is of type U
.
fun
is a function of type int () const &
: a const &
-qualified function taking no parameters and returning int
, and it's a member of class A
. Therefore, in the deduction, T
is deduced to A
and U
is deduced to the type of A::fun
, which is int () const &
.
It may look a bit confusing, because if the type of &A::fun
was spelled out explicitly, it would have to be written int (A::*)() const &
. However, in the template's case, the type int () const &
is "hidden" behind the name U
, so the pointer to member is then just U A::*
. It's similar to how type names can be used to simplify the syntax of normal function pointers:
int foo(char, double) return 42;
using Fun = int (char, double);
Fun *p = &foo;
// instead of:
int (*q)(char, double) = &foo;
The same happens in the template, just with A::*
instead of *
.
I can writeint(A::* p)() const & = &A::fun;
butint () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?
– willtunnels
8 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can writeusing V = int () const &; V A::* p = &A::fun;
. It's the same as writingconst int *p;
vs.using T = int*; const T p;
.
– Angew
7 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat outcannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)
– willtunnels
6 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
add a comment |
U T::*
is a type such that when we have U T::* p
, p
points to a member of class T
, and that member is of type U
.
fun
is a function of type int () const &
: a const &
-qualified function taking no parameters and returning int
, and it's a member of class A
. Therefore, in the deduction, T
is deduced to A
and U
is deduced to the type of A::fun
, which is int () const &
.
It may look a bit confusing, because if the type of &A::fun
was spelled out explicitly, it would have to be written int (A::*)() const &
. However, in the template's case, the type int () const &
is "hidden" behind the name U
, so the pointer to member is then just U A::*
. It's similar to how type names can be used to simplify the syntax of normal function pointers:
int foo(char, double) return 42;
using Fun = int (char, double);
Fun *p = &foo;
// instead of:
int (*q)(char, double) = &foo;
The same happens in the template, just with A::*
instead of *
.
U T::*
is a type such that when we have U T::* p
, p
points to a member of class T
, and that member is of type U
.
fun
is a function of type int () const &
: a const &
-qualified function taking no parameters and returning int
, and it's a member of class A
. Therefore, in the deduction, T
is deduced to A
and U
is deduced to the type of A::fun
, which is int () const &
.
It may look a bit confusing, because if the type of &A::fun
was spelled out explicitly, it would have to be written int (A::*)() const &
. However, in the template's case, the type int () const &
is "hidden" behind the name U
, so the pointer to member is then just U A::*
. It's similar to how type names can be used to simplify the syntax of normal function pointers:
int foo(char, double) return 42;
using Fun = int (char, double);
Fun *p = &foo;
// instead of:
int (*q)(char, double) = &foo;
The same happens in the template, just with A::*
instead of *
.
edited 7 hours ago
answered 8 hours ago
AngewAngew
138k11 gold badges273 silver badges363 bronze badges
138k11 gold badges273 silver badges363 bronze badges
I can writeint(A::* p)() const & = &A::fun;
butint () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?
– willtunnels
8 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can writeusing V = int () const &; V A::* p = &A::fun;
. It's the same as writingconst int *p;
vs.using T = int*; const T p;
.
– Angew
7 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat outcannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)
– willtunnels
6 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
add a comment |
I can writeint(A::* p)() const & = &A::fun;
butint () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?
– willtunnels
8 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can writeusing V = int () const &; V A::* p = &A::fun;
. It's the same as writingconst int *p;
vs.using T = int*; const T p;
.
– Angew
7 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat outcannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)
– willtunnels
6 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
I can write
int(A::* p)() const & = &A::fun;
but int () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?– willtunnels
8 hours ago
I can write
int(A::* p)() const & = &A::fun;
but int () const & A::* p = &A::fun;
gives me an error. Why is there a discrepancy between what happens here and what happens in templates?– willtunnels
8 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can write
using V = int () const &; V A::* p = &A::fun;
. It's the same as writing const int *p;
vs. using T = int*; const T p;
.– Angew
7 hours ago
@willtunnels Because that's how C++ syntax works. Notice that you can write
using V = int () const &; V A::* p = &A::fun;
. It's the same as writing const int *p;
vs. using T = int*; const T p;
.– Angew
7 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat out
cannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)– willtunnels
6 hours ago
That generally seems to work except when ref qualifiers are introduced. I put in your code exactly and g++ spat out
cannot convert 'int (A::*)() const &' to 'int (A::*)() const' in initialization
. Am I missing something obvious? Can someone confirm that that code actually works for them? (Thanks for your help by the way.)– willtunnels
6 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
So, based on some experimentation, it looks like clang handles this correctly (?) but gcc fails.
– willtunnels
5 hours ago
add a comment |
willtunnels is a new contributor. Be nice, and check out our Code of Conduct.
willtunnels is a new contributor. Be nice, and check out our Code of Conduct.
willtunnels is a new contributor. Be nice, and check out our Code of Conduct.
willtunnels is a new contributor. Be nice, and check out our Code of Conduct.
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%2f56709483%2fremoving-class-pointer-from-member-function-pointer-type%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
I've verified that this is the deduced type with g++.
T
is in factint() const&
.– willtunnels
8 hours ago
Well, it seems you are right. But I have a hard time understanding what a
int() const&
can possibly mean outside the context of a member function.const
and the ref qualifier&
are meaningless (and forbidden) on a free function.– François Andrieux
8 hours ago
@FrançoisAndrieux you can simply it, by making a simple member function, non-const. It would still be a function type.
– SergeyA
8 hours ago
1
@FrançoisAndrieux Yes, they are forbidden on free functions, but not on function types in general. Remember that it's legal to declare (not define) a member function using a typedef, for example. Live example
– Angew
8 hours ago
@Angew That's fascinating, thank you for the example.
– François Andrieux
8 hours ago