Error calling templated pointer-to-member function with a return type Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?boost::bind & boost::function pointers to overloaded or templated member functionsIs it possible to have a function pointer to a template function in c++?Static member function pointer as template argumentIs there any guarantee on the order of substitution in a function template after type deduction?Pointer to a template member with auto return 'type' in c++?error defining std::function pointer to an instance of a function template, which is a member of a templated class?Can a function templates return type depend on the same function templates return type recursively?Array of function pointers ( including member functions) throwing template specialization errorHow to create thread with template member function of a template object in c++How does this non-type template parameter expansion work? + Possible bug in gcc?
Why is "Captain Marvel" translated as male in Portugal?
Is the Standard Deduction better than Itemized when both are the same amount?
Did Kevin spill real chili?
Storing hydrofluoric acid before the invention of plastics
Is it true that "carbohydrates are of no use for the basal metabolic need"?
Is there a concise way to say "all of the X, one of each"?
Why did the IBM 650 use bi-quinary?
What is this single-engine low-wing propeller plane?
Check which numbers satisfy the condition [A*B*C = A! + B! + C!]
Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?
Should gear shift center itself while in neutral?
Letter Boxed validator
How to motivate offshore teams and trust them to deliver?
Models of set theory where not every set can be linearly ordered
Does surprise arrest existing movement?
Is 1 ppb equal to 1 μg/kg?
Why are there no cargo aircraft with "flying wing" design?
Stars Make Stars
What does the "x" in "x86" represent?
What is a Meta algorithm?
What is the musical term for a note that continously plays through a melody?
Right-skewed distribution with mean equals to mode?
Bonus calculation: Am I making a mountain out of a molehill?
How discoverable are IPv6 addresses and AAAA names by potential attackers?
Error calling templated pointer-to-member function with a return type
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?boost::bind & boost::function pointers to overloaded or templated member functionsIs it possible to have a function pointer to a template function in c++?Static member function pointer as template argumentIs there any guarantee on the order of substitution in a function template after type deduction?Pointer to a template member with auto return 'type' in c++?error defining std::function pointer to an instance of a function template, which is a member of a templated class?Can a function templates return type depend on the same function templates return type recursively?Array of function pointers ( including member functions) throwing template specialization errorHow to create thread with template member function of a template object in c++How does this non-type template parameter expansion work? + Possible bug in gcc?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
template<typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype(h(args...))
T* t = new T(); // Don't worry, my actual code doesn't do this
return (t->h)(args...);
struct G
int g()
return 5;
;
int main()
int k = f<G>(&G::g);
Microsoft's compiler says error C2672: 'f': no matching overloaded function found
and error C2893: Failed to specialize function template 'unknown-type f(F,Args &&...)'
.
Clang's compiler says note: candidate template ignored: substitution failure [with T = G, F = int (G::*)(), Args = <>]: called object type 'int (G::*)()' is not a function or function pointer
and error: no matching function for call to 'f'
.
I'm pretty sure int (G::*)()
is a function pointer ... ? What am I missing? (All of this worked fine before I added the return type.)
c++ templates variadic-templates member-function-pointers template-deduction
add a comment |
template<typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype(h(args...))
T* t = new T(); // Don't worry, my actual code doesn't do this
return (t->h)(args...);
struct G
int g()
return 5;
;
int main()
int k = f<G>(&G::g);
Microsoft's compiler says error C2672: 'f': no matching overloaded function found
and error C2893: Failed to specialize function template 'unknown-type f(F,Args &&...)'
.
Clang's compiler says note: candidate template ignored: substitution failure [with T = G, F = int (G::*)(), Args = <>]: called object type 'int (G::*)()' is not a function or function pointer
and error: no matching function for call to 'f'
.
I'm pretty sure int (G::*)()
is a function pointer ... ? What am I missing? (All of this worked fine before I added the return type.)
c++ templates variadic-templates member-function-pointers template-deduction
1
Compiler is telling you all. You can't callh(...)
, becauseh
is not callable on itself whenh
is a pointer-to-member. If you are using C++14, easiest would be to simply omit traliling return type.
– SergeyA
Mar 8 at 16:20
I don't understand how that worked. What do you mean by h not being callable on itself?
– Jorge Rodriguez
Mar 8 at 16:31
Oh wait I get it, you can't call just h() you need t->h.
– Jorge Rodriguez
Mar 8 at 16:33
You can use something likedecltype((std::declval<T>().*h)(args...))
– super
Mar 8 at 17:22
add a comment |
template<typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype(h(args...))
T* t = new T(); // Don't worry, my actual code doesn't do this
return (t->h)(args...);
struct G
int g()
return 5;
;
int main()
int k = f<G>(&G::g);
Microsoft's compiler says error C2672: 'f': no matching overloaded function found
and error C2893: Failed to specialize function template 'unknown-type f(F,Args &&...)'
.
Clang's compiler says note: candidate template ignored: substitution failure [with T = G, F = int (G::*)(), Args = <>]: called object type 'int (G::*)()' is not a function or function pointer
and error: no matching function for call to 'f'
.
I'm pretty sure int (G::*)()
is a function pointer ... ? What am I missing? (All of this worked fine before I added the return type.)
c++ templates variadic-templates member-function-pointers template-deduction
template<typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype(h(args...))
T* t = new T(); // Don't worry, my actual code doesn't do this
return (t->h)(args...);
struct G
int g()
return 5;
;
int main()
int k = f<G>(&G::g);
Microsoft's compiler says error C2672: 'f': no matching overloaded function found
and error C2893: Failed to specialize function template 'unknown-type f(F,Args &&...)'
.
Clang's compiler says note: candidate template ignored: substitution failure [with T = G, F = int (G::*)(), Args = <>]: called object type 'int (G::*)()' is not a function or function pointer
and error: no matching function for call to 'f'
.
I'm pretty sure int (G::*)()
is a function pointer ... ? What am I missing? (All of this worked fine before I added the return type.)
c++ templates variadic-templates member-function-pointers template-deduction
c++ templates variadic-templates member-function-pointers template-deduction
edited Mar 8 at 21:31
max66
39.4k74575
39.4k74575
asked Mar 8 at 16:17
Jorge RodriguezJorge Rodriguez
299411
299411
1
Compiler is telling you all. You can't callh(...)
, becauseh
is not callable on itself whenh
is a pointer-to-member. If you are using C++14, easiest would be to simply omit traliling return type.
– SergeyA
Mar 8 at 16:20
I don't understand how that worked. What do you mean by h not being callable on itself?
– Jorge Rodriguez
Mar 8 at 16:31
Oh wait I get it, you can't call just h() you need t->h.
– Jorge Rodriguez
Mar 8 at 16:33
You can use something likedecltype((std::declval<T>().*h)(args...))
– super
Mar 8 at 17:22
add a comment |
1
Compiler is telling you all. You can't callh(...)
, becauseh
is not callable on itself whenh
is a pointer-to-member. If you are using C++14, easiest would be to simply omit traliling return type.
– SergeyA
Mar 8 at 16:20
I don't understand how that worked. What do you mean by h not being callable on itself?
– Jorge Rodriguez
Mar 8 at 16:31
Oh wait I get it, you can't call just h() you need t->h.
– Jorge Rodriguez
Mar 8 at 16:33
You can use something likedecltype((std::declval<T>().*h)(args...))
– super
Mar 8 at 17:22
1
1
Compiler is telling you all. You can't call
h(...)
, because h
is not callable on itself when h
is a pointer-to-member. If you are using C++14, easiest would be to simply omit traliling return type.– SergeyA
Mar 8 at 16:20
Compiler is telling you all. You can't call
h(...)
, because h
is not callable on itself when h
is a pointer-to-member. If you are using C++14, easiest would be to simply omit traliling return type.– SergeyA
Mar 8 at 16:20
I don't understand how that worked. What do you mean by h not being callable on itself?
– Jorge Rodriguez
Mar 8 at 16:31
I don't understand how that worked. What do you mean by h not being callable on itself?
– Jorge Rodriguez
Mar 8 at 16:31
Oh wait I get it, you can't call just h() you need t->h.
– Jorge Rodriguez
Mar 8 at 16:33
Oh wait I get it, you can't call just h() you need t->h.
– Jorge Rodriguez
Mar 8 at 16:33
You can use something like
decltype((std::declval<T>().*h)(args...))
– super
Mar 8 at 17:22
You can use something like
decltype((std::declval<T>().*h)(args...))
– super
Mar 8 at 17:22
add a comment |
1 Answer
1
active
oldest
votes
I'm pretty sure
int (G::*)()
is a function pointer ... ? What am I missing?
Non exactly: int (G::*)()
is a pointer to a non-static method. That isn't exactly the same things and require a little different syntax to call it.
So, instead of
return (t->h)(args...);
you should add a *
and call h()
as follows
return (t->*h)(args...);
// ........^ add this *
The decltype()
is also wrong. If you can use at least C++14, you can avoid it and simply use auto
as return type
template <typename T, typename F, typename ...Args>
auto f (F h, Args&&... args)
T* t = new T();
return (t->*h)(args...);
otherwise, if you must use C++11, you can include <utility>
and use std::declval()
as follows
template <typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype((std::declval<T*>()->*h)(args...))
T* t = new T(); // .................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return (t->*h)(args...);
But there is another way to write your f()
function: deducing the returned type (so avoiding the auto
, the decltype()
and the std::declval()
) and the arguments of h()
You can write f()
as follows
template<typename R, typename T, typename ... As1, typename ... As2>
R f(R(T::*h)(As1...), As2 && ... args)
T* t = new T();
return (t->*h)(args...);
and you avoid to explicit the G
type calling it
int k = f(&G::g);
// .....^^^^^^^^ no more explicit <G> needed
because the T
template type is deduced from the argument &G::g
.
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%2f55067069%2ferror-calling-templated-pointer-to-member-function-with-a-return-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
I'm pretty sure
int (G::*)()
is a function pointer ... ? What am I missing?
Non exactly: int (G::*)()
is a pointer to a non-static method. That isn't exactly the same things and require a little different syntax to call it.
So, instead of
return (t->h)(args...);
you should add a *
and call h()
as follows
return (t->*h)(args...);
// ........^ add this *
The decltype()
is also wrong. If you can use at least C++14, you can avoid it and simply use auto
as return type
template <typename T, typename F, typename ...Args>
auto f (F h, Args&&... args)
T* t = new T();
return (t->*h)(args...);
otherwise, if you must use C++11, you can include <utility>
and use std::declval()
as follows
template <typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype((std::declval<T*>()->*h)(args...))
T* t = new T(); // .................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return (t->*h)(args...);
But there is another way to write your f()
function: deducing the returned type (so avoiding the auto
, the decltype()
and the std::declval()
) and the arguments of h()
You can write f()
as follows
template<typename R, typename T, typename ... As1, typename ... As2>
R f(R(T::*h)(As1...), As2 && ... args)
T* t = new T();
return (t->*h)(args...);
and you avoid to explicit the G
type calling it
int k = f(&G::g);
// .....^^^^^^^^ no more explicit <G> needed
because the T
template type is deduced from the argument &G::g
.
add a comment |
I'm pretty sure
int (G::*)()
is a function pointer ... ? What am I missing?
Non exactly: int (G::*)()
is a pointer to a non-static method. That isn't exactly the same things and require a little different syntax to call it.
So, instead of
return (t->h)(args...);
you should add a *
and call h()
as follows
return (t->*h)(args...);
// ........^ add this *
The decltype()
is also wrong. If you can use at least C++14, you can avoid it and simply use auto
as return type
template <typename T, typename F, typename ...Args>
auto f (F h, Args&&... args)
T* t = new T();
return (t->*h)(args...);
otherwise, if you must use C++11, you can include <utility>
and use std::declval()
as follows
template <typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype((std::declval<T*>()->*h)(args...))
T* t = new T(); // .................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return (t->*h)(args...);
But there is another way to write your f()
function: deducing the returned type (so avoiding the auto
, the decltype()
and the std::declval()
) and the arguments of h()
You can write f()
as follows
template<typename R, typename T, typename ... As1, typename ... As2>
R f(R(T::*h)(As1...), As2 && ... args)
T* t = new T();
return (t->*h)(args...);
and you avoid to explicit the G
type calling it
int k = f(&G::g);
// .....^^^^^^^^ no more explicit <G> needed
because the T
template type is deduced from the argument &G::g
.
add a comment |
I'm pretty sure
int (G::*)()
is a function pointer ... ? What am I missing?
Non exactly: int (G::*)()
is a pointer to a non-static method. That isn't exactly the same things and require a little different syntax to call it.
So, instead of
return (t->h)(args...);
you should add a *
and call h()
as follows
return (t->*h)(args...);
// ........^ add this *
The decltype()
is also wrong. If you can use at least C++14, you can avoid it and simply use auto
as return type
template <typename T, typename F, typename ...Args>
auto f (F h, Args&&... args)
T* t = new T();
return (t->*h)(args...);
otherwise, if you must use C++11, you can include <utility>
and use std::declval()
as follows
template <typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype((std::declval<T*>()->*h)(args...))
T* t = new T(); // .................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return (t->*h)(args...);
But there is another way to write your f()
function: deducing the returned type (so avoiding the auto
, the decltype()
and the std::declval()
) and the arguments of h()
You can write f()
as follows
template<typename R, typename T, typename ... As1, typename ... As2>
R f(R(T::*h)(As1...), As2 && ... args)
T* t = new T();
return (t->*h)(args...);
and you avoid to explicit the G
type calling it
int k = f(&G::g);
// .....^^^^^^^^ no more explicit <G> needed
because the T
template type is deduced from the argument &G::g
.
I'm pretty sure
int (G::*)()
is a function pointer ... ? What am I missing?
Non exactly: int (G::*)()
is a pointer to a non-static method. That isn't exactly the same things and require a little different syntax to call it.
So, instead of
return (t->h)(args...);
you should add a *
and call h()
as follows
return (t->*h)(args...);
// ........^ add this *
The decltype()
is also wrong. If you can use at least C++14, you can avoid it and simply use auto
as return type
template <typename T, typename F, typename ...Args>
auto f (F h, Args&&... args)
T* t = new T();
return (t->*h)(args...);
otherwise, if you must use C++11, you can include <utility>
and use std::declval()
as follows
template <typename T, typename F, typename ...Args>
auto f(F h, Args&&... args) -> decltype((std::declval<T*>()->*h)(args...))
T* t = new T(); // .................^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
return (t->*h)(args...);
But there is another way to write your f()
function: deducing the returned type (so avoiding the auto
, the decltype()
and the std::declval()
) and the arguments of h()
You can write f()
as follows
template<typename R, typename T, typename ... As1, typename ... As2>
R f(R(T::*h)(As1...), As2 && ... args)
T* t = new T();
return (t->*h)(args...);
and you avoid to explicit the G
type calling it
int k = f(&G::g);
// .....^^^^^^^^ no more explicit <G> needed
because the T
template type is deduced from the argument &G::g
.
edited Mar 8 at 21:32
answered Mar 8 at 18:17
max66max66
39.4k74575
39.4k74575
add a comment |
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%2f55067069%2ferror-calling-templated-pointer-to-member-function-with-a-return-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
1
Compiler is telling you all. You can't call
h(...)
, becauseh
is not callable on itself whenh
is a pointer-to-member. If you are using C++14, easiest would be to simply omit traliling return type.– SergeyA
Mar 8 at 16:20
I don't understand how that worked. What do you mean by h not being callable on itself?
– Jorge Rodriguez
Mar 8 at 16:31
Oh wait I get it, you can't call just h() you need t->h.
– Jorge Rodriguez
Mar 8 at 16:33
You can use something like
decltype((std::declval<T>().*h)(args...))
– super
Mar 8 at 17:22