How to remove decltype(&MyClass::funct) part by extending the following type traits?Can I get the Owning Object of a Member Function Template Parameter?Pretty-print C++ STL containersRemove reference in decltype (return T instead of T& where T& is the decltype)Return type deduction: What method is preferred?Specialize function template with decltype trailing return typetypedef the following type : Pointer to a member function Fof “any” class having a member function Fdecltype and const reference return types for stringsIs “#define TYPE(x) typename decltype(x)” a bad idea?Class templated member function and return type inferencetype traits for aggregatesType trait for aggregate initializability in the standard library?
On a piano, are the effects of holding notes and the sustain pedal the same for a single chord?
Why are Marine Le Pen's possible connections with Steve Bannon something worth investigating?
Why is python script running in background consuming 100 % CPU?
Latin words remembered from high school 50 years ago
Is it possible to view all the attribute data in QGIS
Hotel booking: Why is Agoda much cheaper than booking.com?
Is there any official Lore on Keraptis the Wizard, apart from what is in White Plume Mountain?
What's is the easiest way to purchase a stock and hold it
Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?
Is being an extrovert a necessary condition to be a manager?
Character had a different name in the past. Which name should I use in a flashback?
Failing students when it might cause them economic ruin
Why could the Lunar Ascent Engine be used only once?
How can sister protect herself from impulse purchases with a credit card?
What city and town structures are important in a low fantasy medieval world?
Could a chemically propelled craft travel directly between Earth and Mars spaceports?
How does the "reverse syntax" in Middle English work?
Are there any crystals that are theoretically possible, but haven't yet been made?
Addressing an email
What does this 'x' mean on the stem of the voice's note, above the notehead?
Have I found a major security issue with login
How do I unravel apparent recursion in an edef statement?
Can 2 light bulbs of 120V in series be used on 230V AC?
What should I wear to go and sign an employment contract?
How to remove decltype(&MyClass::funct) part by extending the following type traits?
Can I get the Owning Object of a Member Function Template Parameter?Pretty-print C++ STL containersRemove reference in decltype (return T instead of T& where T& is the decltype)Return type deduction: What method is preferred?Specialize function template with decltype trailing return typetypedef the following type : Pointer to a member function Fof “any” class having a member function Fdecltype and const reference return types for stringsIs “#define TYPE(x) typename decltype(x)” a bad idea?Class templated member function and return type inferencetype traits for aggregatesType trait for aggregate initializability in the standard library?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I wanted to have type traits which will help me to get the type of the class
from a member function pointer. I looked into this answer
and found my half way to the aim.
It looks like this:
#include <iostream>
// example class
struct MyClass
void funct() std::cout << "funct has been called....n";
;
// traits
template<typename Class> struct get_class;
template<typename ReType, typename Class, typename... Args>
struct get_class<ReType(Class::*)(Args...)>
using type = Class;
;
template<typename Type> using get_class_t = typename get_class<Type>::type;
int main()
get_class_t<decltype(&MyClass::funct)> myObj;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---> this is a lot of typing
myObj.funct();
return 0;
But, as shown in the code I need every time to write get_class_t<decltype(&MyClass::funct)>
or in the case of
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
// ^^^^^^^^^^^^^^
which is a lot of decltype()
ing. I would like to write instead
class_t<ptr> obj;
or
class_t<&MyClass::funct> myObj;
which is more convenient.
I did the following function, which will return a resulting object of the class
and maybe I could do, want I wanted to.
template<typename Type>
auto helper_function(Type ptr)->get_class_t<Type>
return get_class_t<Type>;
template<typename Type>
using class_t = /* decltype(helper_function(Type ptr));*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // what could be here?
I do not know how to complete this. My goal is to extend the traits in such a way that I
could create an object like
auto ptr = &MyClass::funct;
class_t<ptr> myObj;
// or
class_t<&MyClass::funct> myObj;
Is there any other way to do this? or should I have to stick with decltype()
ing?
As I have tagged, I would like to see whether it possible with C++11?
c++ c++11 templates typetraits member-function-pointers
|
show 1 more comment
I wanted to have type traits which will help me to get the type of the class
from a member function pointer. I looked into this answer
and found my half way to the aim.
It looks like this:
#include <iostream>
// example class
struct MyClass
void funct() std::cout << "funct has been called....n";
;
// traits
template<typename Class> struct get_class;
template<typename ReType, typename Class, typename... Args>
struct get_class<ReType(Class::*)(Args...)>
using type = Class;
;
template<typename Type> using get_class_t = typename get_class<Type>::type;
int main()
get_class_t<decltype(&MyClass::funct)> myObj;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---> this is a lot of typing
myObj.funct();
return 0;
But, as shown in the code I need every time to write get_class_t<decltype(&MyClass::funct)>
or in the case of
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
// ^^^^^^^^^^^^^^
which is a lot of decltype()
ing. I would like to write instead
class_t<ptr> obj;
or
class_t<&MyClass::funct> myObj;
which is more convenient.
I did the following function, which will return a resulting object of the class
and maybe I could do, want I wanted to.
template<typename Type>
auto helper_function(Type ptr)->get_class_t<Type>
return get_class_t<Type>;
template<typename Type>
using class_t = /* decltype(helper_function(Type ptr));*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // what could be here?
I do not know how to complete this. My goal is to extend the traits in such a way that I
could create an object like
auto ptr = &MyClass::funct;
class_t<ptr> myObj;
// or
class_t<&MyClass::funct> myObj;
Is there any other way to do this? or should I have to stick with decltype()
ing?
As I have tagged, I would like to see whether it possible with C++11?
c++ c++11 templates typetraits member-function-pointers
2
As I tagged I would like to see whether it possible with C++11?
– UsingCpp
May 13 at 10:21
5
@Someprogrammerdude IMO if one tags the specific version of the standard that person want's an answer for that standard. I think it's always crucial
– Timo
May 13 at 10:26
@Timo, the OP and others: Many beginners and newbies (and sometime intermediate knowledge posters) very often put specific standard-version tags in their question, when their questions could be about generic C++. It's impossible without explicit mentioning to know if it's deliberate or by mistake.
– Some programmer dude
May 13 at 10:31
No, this is not really possible in C++11.
– L. F.
May 13 at 10:47
1
@Someprogrammerdude Then err on the side of caution by asking first if you're not sure!!
– Lightness Races in Orbit
May 13 at 10:59
|
show 1 more comment
I wanted to have type traits which will help me to get the type of the class
from a member function pointer. I looked into this answer
and found my half way to the aim.
It looks like this:
#include <iostream>
// example class
struct MyClass
void funct() std::cout << "funct has been called....n";
;
// traits
template<typename Class> struct get_class;
template<typename ReType, typename Class, typename... Args>
struct get_class<ReType(Class::*)(Args...)>
using type = Class;
;
template<typename Type> using get_class_t = typename get_class<Type>::type;
int main()
get_class_t<decltype(&MyClass::funct)> myObj;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---> this is a lot of typing
myObj.funct();
return 0;
But, as shown in the code I need every time to write get_class_t<decltype(&MyClass::funct)>
or in the case of
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
// ^^^^^^^^^^^^^^
which is a lot of decltype()
ing. I would like to write instead
class_t<ptr> obj;
or
class_t<&MyClass::funct> myObj;
which is more convenient.
I did the following function, which will return a resulting object of the class
and maybe I could do, want I wanted to.
template<typename Type>
auto helper_function(Type ptr)->get_class_t<Type>
return get_class_t<Type>;
template<typename Type>
using class_t = /* decltype(helper_function(Type ptr));*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // what could be here?
I do not know how to complete this. My goal is to extend the traits in such a way that I
could create an object like
auto ptr = &MyClass::funct;
class_t<ptr> myObj;
// or
class_t<&MyClass::funct> myObj;
Is there any other way to do this? or should I have to stick with decltype()
ing?
As I have tagged, I would like to see whether it possible with C++11?
c++ c++11 templates typetraits member-function-pointers
I wanted to have type traits which will help me to get the type of the class
from a member function pointer. I looked into this answer
and found my half way to the aim.
It looks like this:
#include <iostream>
// example class
struct MyClass
void funct() std::cout << "funct has been called....n";
;
// traits
template<typename Class> struct get_class;
template<typename ReType, typename Class, typename... Args>
struct get_class<ReType(Class::*)(Args...)>
using type = Class;
;
template<typename Type> using get_class_t = typename get_class<Type>::type;
int main()
get_class_t<decltype(&MyClass::funct)> myObj;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ---> this is a lot of typing
myObj.funct();
return 0;
But, as shown in the code I need every time to write get_class_t<decltype(&MyClass::funct)>
or in the case of
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
// ^^^^^^^^^^^^^^
which is a lot of decltype()
ing. I would like to write instead
class_t<ptr> obj;
or
class_t<&MyClass::funct> myObj;
which is more convenient.
I did the following function, which will return a resulting object of the class
and maybe I could do, want I wanted to.
template<typename Type>
auto helper_function(Type ptr)->get_class_t<Type>
return get_class_t<Type>;
template<typename Type>
using class_t = /* decltype(helper_function(Type ptr));*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // what could be here?
I do not know how to complete this. My goal is to extend the traits in such a way that I
could create an object like
auto ptr = &MyClass::funct;
class_t<ptr> myObj;
// or
class_t<&MyClass::funct> myObj;
Is there any other way to do this? or should I have to stick with decltype()
ing?
As I have tagged, I would like to see whether it possible with C++11?
c++ c++11 templates typetraits member-function-pointers
c++ c++11 templates typetraits member-function-pointers
edited May 13 at 20:24
Failed Scientist
1,49432135
1,49432135
asked May 13 at 10:09
UsingCppUsingCpp
165114
165114
2
As I tagged I would like to see whether it possible with C++11?
– UsingCpp
May 13 at 10:21
5
@Someprogrammerdude IMO if one tags the specific version of the standard that person want's an answer for that standard. I think it's always crucial
– Timo
May 13 at 10:26
@Timo, the OP and others: Many beginners and newbies (and sometime intermediate knowledge posters) very often put specific standard-version tags in their question, when their questions could be about generic C++. It's impossible without explicit mentioning to know if it's deliberate or by mistake.
– Some programmer dude
May 13 at 10:31
No, this is not really possible in C++11.
– L. F.
May 13 at 10:47
1
@Someprogrammerdude Then err on the side of caution by asking first if you're not sure!!
– Lightness Races in Orbit
May 13 at 10:59
|
show 1 more comment
2
As I tagged I would like to see whether it possible with C++11?
– UsingCpp
May 13 at 10:21
5
@Someprogrammerdude IMO if one tags the specific version of the standard that person want's an answer for that standard. I think it's always crucial
– Timo
May 13 at 10:26
@Timo, the OP and others: Many beginners and newbies (and sometime intermediate knowledge posters) very often put specific standard-version tags in their question, when their questions could be about generic C++. It's impossible without explicit mentioning to know if it's deliberate or by mistake.
– Some programmer dude
May 13 at 10:31
No, this is not really possible in C++11.
– L. F.
May 13 at 10:47
1
@Someprogrammerdude Then err on the side of caution by asking first if you're not sure!!
– Lightness Races in Orbit
May 13 at 10:59
2
2
As I tagged I would like to see whether it possible with C++11?
– UsingCpp
May 13 at 10:21
As I tagged I would like to see whether it possible with C++11?
– UsingCpp
May 13 at 10:21
5
5
@Someprogrammerdude IMO if one tags the specific version of the standard that person want's an answer for that standard. I think it's always crucial
– Timo
May 13 at 10:26
@Someprogrammerdude IMO if one tags the specific version of the standard that person want's an answer for that standard. I think it's always crucial
– Timo
May 13 at 10:26
@Timo, the OP and others: Many beginners and newbies (and sometime intermediate knowledge posters) very often put specific standard-version tags in their question, when their questions could be about generic C++. It's impossible without explicit mentioning to know if it's deliberate or by mistake.
– Some programmer dude
May 13 at 10:31
@Timo, the OP and others: Many beginners and newbies (and sometime intermediate knowledge posters) very often put specific standard-version tags in their question, when their questions could be about generic C++. It's impossible without explicit mentioning to know if it's deliberate or by mistake.
– Some programmer dude
May 13 at 10:31
No, this is not really possible in C++11.
– L. F.
May 13 at 10:47
No, this is not really possible in C++11.
– L. F.
May 13 at 10:47
1
1
@Someprogrammerdude Then err on the side of caution by asking first if you're not sure!!
– Lightness Races in Orbit
May 13 at 10:59
@Someprogrammerdude Then err on the side of caution by asking first if you're not sure!!
– Lightness Races in Orbit
May 13 at 10:59
|
show 1 more comment
2 Answers
2
active
oldest
votes
You can provide a function which will create required object. This is very simple to achieve:
template<typename T, typename ...Args>
auto makeObjectForMethod(T&&, Args&& ...args) -> get_class_t<decltype(&MyClass::funct)>
using R = get_class_t<decltype(&MyClass::funct)>;
return R std::forward(args)... ;
int main()
auto myObj = makeObjectForMethod(&MyClass::funct);
myObj.funct();
return 0;
Works with C++11 and is quite handy:
https://wandbox.org/permlink/usMa3fA0I2HCNJ7M
The only disadvantage that in case of class fields it is not very helpful.
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
current version works with ctor args, which can be passed as extra arguments ofmakeObjectForMethod
.
– Marek R
May 13 at 12:56
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
add a comment |
(Answer archived for future visitors; this solution requires C++17!)
You're really close!
The trick is auto
template arguments, and the fact that pointers-to-members can be used as template arguments, like so:
template <auto thing>
using class_t = get_class_t<decltype(thing)>;
int main()
class_t<&MyClass::funct> myObj;
myObj.funct();
Of course if you can write this then you already know the type so you'd just write MyClass
, so that's not very useful.
Sadly you won't be able to make it accept ptr
as a template argument, though; you're stuck with get_class_t
for that:
int main()
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
myObj.funct();
(live demo)
In the latter case, a nice type alias can help you a bit:
auto ptr = &MyClass::funct;
using ClassType = get_class_t<decltype(ptr)>;
ClassType myObj;
myObj.funct();
(live demo)
Personally I think this level of verbosity is pretty reasonable.
1
I have already so that in the linked post. Butauto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh
– UsingCpp
May 13 at 10:20
2
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
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%2f56110190%2fhow-to-remove-decltypemyclassfunct-part-by-extending-the-following-type-tra%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
You can provide a function which will create required object. This is very simple to achieve:
template<typename T, typename ...Args>
auto makeObjectForMethod(T&&, Args&& ...args) -> get_class_t<decltype(&MyClass::funct)>
using R = get_class_t<decltype(&MyClass::funct)>;
return R std::forward(args)... ;
int main()
auto myObj = makeObjectForMethod(&MyClass::funct);
myObj.funct();
return 0;
Works with C++11 and is quite handy:
https://wandbox.org/permlink/usMa3fA0I2HCNJ7M
The only disadvantage that in case of class fields it is not very helpful.
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
current version works with ctor args, which can be passed as extra arguments ofmakeObjectForMethod
.
– Marek R
May 13 at 12:56
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
add a comment |
You can provide a function which will create required object. This is very simple to achieve:
template<typename T, typename ...Args>
auto makeObjectForMethod(T&&, Args&& ...args) -> get_class_t<decltype(&MyClass::funct)>
using R = get_class_t<decltype(&MyClass::funct)>;
return R std::forward(args)... ;
int main()
auto myObj = makeObjectForMethod(&MyClass::funct);
myObj.funct();
return 0;
Works with C++11 and is quite handy:
https://wandbox.org/permlink/usMa3fA0I2HCNJ7M
The only disadvantage that in case of class fields it is not very helpful.
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
current version works with ctor args, which can be passed as extra arguments ofmakeObjectForMethod
.
– Marek R
May 13 at 12:56
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
add a comment |
You can provide a function which will create required object. This is very simple to achieve:
template<typename T, typename ...Args>
auto makeObjectForMethod(T&&, Args&& ...args) -> get_class_t<decltype(&MyClass::funct)>
using R = get_class_t<decltype(&MyClass::funct)>;
return R std::forward(args)... ;
int main()
auto myObj = makeObjectForMethod(&MyClass::funct);
myObj.funct();
return 0;
Works with C++11 and is quite handy:
https://wandbox.org/permlink/usMa3fA0I2HCNJ7M
The only disadvantage that in case of class fields it is not very helpful.
You can provide a function which will create required object. This is very simple to achieve:
template<typename T, typename ...Args>
auto makeObjectForMethod(T&&, Args&& ...args) -> get_class_t<decltype(&MyClass::funct)>
using R = get_class_t<decltype(&MyClass::funct)>;
return R std::forward(args)... ;
int main()
auto myObj = makeObjectForMethod(&MyClass::funct);
myObj.funct();
return 0;
Works with C++11 and is quite handy:
https://wandbox.org/permlink/usMa3fA0I2HCNJ7M
The only disadvantage that in case of class fields it is not very helpful.
edited May 13 at 14:15
answered May 13 at 11:07
Marek RMarek R
14.2k22879
14.2k22879
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
current version works with ctor args, which can be passed as extra arguments ofmakeObjectForMethod
.
– Marek R
May 13 at 12:56
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
add a comment |
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
current version works with ctor args, which can be passed as extra arguments ofmakeObjectForMethod
.
– Marek R
May 13 at 12:56
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
A good solution iff there are no ctor args. Not a general solution but I suppose that may not matter here.
– Lightness Races in Orbit
May 13 at 12:21
current version works with ctor args, which can be passed as extra arguments of
makeObjectForMethod
.– Marek R
May 13 at 12:56
current version works with ctor args, which can be passed as extra arguments of
makeObjectForMethod
.– Marek R
May 13 at 12:56
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
Better! Still not sure I'd use this in reality but it's a valid workaround to not being able to get just the type. Just be careful of potential copies on return if the thingie isn't moveable (and RVO doesn't kick in) cos this is pre-C++17
– Lightness Races in Orbit
May 13 at 13:09
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
I realize there is no way out for C++11 compiler but this could make it better, but not the best.
– UsingCpp
May 14 at 7:00
add a comment |
(Answer archived for future visitors; this solution requires C++17!)
You're really close!
The trick is auto
template arguments, and the fact that pointers-to-members can be used as template arguments, like so:
template <auto thing>
using class_t = get_class_t<decltype(thing)>;
int main()
class_t<&MyClass::funct> myObj;
myObj.funct();
Of course if you can write this then you already know the type so you'd just write MyClass
, so that's not very useful.
Sadly you won't be able to make it accept ptr
as a template argument, though; you're stuck with get_class_t
for that:
int main()
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
myObj.funct();
(live demo)
In the latter case, a nice type alias can help you a bit:
auto ptr = &MyClass::funct;
using ClassType = get_class_t<decltype(ptr)>;
ClassType myObj;
myObj.funct();
(live demo)
Personally I think this level of verbosity is pretty reasonable.
1
I have already so that in the linked post. Butauto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh
– UsingCpp
May 13 at 10:20
2
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
add a comment |
(Answer archived for future visitors; this solution requires C++17!)
You're really close!
The trick is auto
template arguments, and the fact that pointers-to-members can be used as template arguments, like so:
template <auto thing>
using class_t = get_class_t<decltype(thing)>;
int main()
class_t<&MyClass::funct> myObj;
myObj.funct();
Of course if you can write this then you already know the type so you'd just write MyClass
, so that's not very useful.
Sadly you won't be able to make it accept ptr
as a template argument, though; you're stuck with get_class_t
for that:
int main()
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
myObj.funct();
(live demo)
In the latter case, a nice type alias can help you a bit:
auto ptr = &MyClass::funct;
using ClassType = get_class_t<decltype(ptr)>;
ClassType myObj;
myObj.funct();
(live demo)
Personally I think this level of verbosity is pretty reasonable.
1
I have already so that in the linked post. Butauto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh
– UsingCpp
May 13 at 10:20
2
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
add a comment |
(Answer archived for future visitors; this solution requires C++17!)
You're really close!
The trick is auto
template arguments, and the fact that pointers-to-members can be used as template arguments, like so:
template <auto thing>
using class_t = get_class_t<decltype(thing)>;
int main()
class_t<&MyClass::funct> myObj;
myObj.funct();
Of course if you can write this then you already know the type so you'd just write MyClass
, so that's not very useful.
Sadly you won't be able to make it accept ptr
as a template argument, though; you're stuck with get_class_t
for that:
int main()
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
myObj.funct();
(live demo)
In the latter case, a nice type alias can help you a bit:
auto ptr = &MyClass::funct;
using ClassType = get_class_t<decltype(ptr)>;
ClassType myObj;
myObj.funct();
(live demo)
Personally I think this level of verbosity is pretty reasonable.
(Answer archived for future visitors; this solution requires C++17!)
You're really close!
The trick is auto
template arguments, and the fact that pointers-to-members can be used as template arguments, like so:
template <auto thing>
using class_t = get_class_t<decltype(thing)>;
int main()
class_t<&MyClass::funct> myObj;
myObj.funct();
Of course if you can write this then you already know the type so you'd just write MyClass
, so that's not very useful.
Sadly you won't be able to make it accept ptr
as a template argument, though; you're stuck with get_class_t
for that:
int main()
auto ptr = &MyClass::funct;
get_class_t<decltype(ptr)> myObj;
myObj.funct();
(live demo)
In the latter case, a nice type alias can help you a bit:
auto ptr = &MyClass::funct;
using ClassType = get_class_t<decltype(ptr)>;
ClassType myObj;
myObj.funct();
(live demo)
Personally I think this level of verbosity is pretty reasonable.
edited May 13 at 10:21
answered May 13 at 10:15
Lightness Races in OrbitLightness Races in Orbit
299k56484831
299k56484831
1
I have already so that in the linked post. Butauto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh
– UsingCpp
May 13 at 10:20
2
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
add a comment |
1
I have already so that in the linked post. Butauto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh
– UsingCpp
May 13 at 10:20
2
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
1
1
I have already so that in the linked post. But
auto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh– UsingCpp
May 13 at 10:20
I have already so that in the linked post. But
auto things
need C++17. As I tagged I would like to see whether it possible with C++11. godbolt.org/z/3v3SVh– UsingCpp
May 13 at 10:20
2
2
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
@UsingCpp Dang, someone removed that tag. Sorry. I'll keep this answer up for posterity but I think you're out of luck then.
– Lightness Races in Orbit
May 13 at 10:22
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%2f56110190%2fhow-to-remove-decltypemyclassfunct-part-by-extending-the-following-type-tra%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
2
As I tagged I would like to see whether it possible with C++11?
– UsingCpp
May 13 at 10:21
5
@Someprogrammerdude IMO if one tags the specific version of the standard that person want's an answer for that standard. I think it's always crucial
– Timo
May 13 at 10:26
@Timo, the OP and others: Many beginners and newbies (and sometime intermediate knowledge posters) very often put specific standard-version tags in their question, when their questions could be about generic C++. It's impossible without explicit mentioning to know if it's deliberate or by mistake.
– Some programmer dude
May 13 at 10:31
No, this is not really possible in C++11.
– L. F.
May 13 at 10:47
1
@Someprogrammerdude Then err on the side of caution by asking first if you're not sure!!
– Lightness Races in Orbit
May 13 at 10:59