Declare new function inside a class using a templateWhat is the difference between old style and new style classes in Python?Are static class variables possible in Python?Use 'class' or 'typename' for template parameters?When to use static classes in C#Is it possible to write a template to check for a function's existence?Why can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?Meaning of 'const' last in a function declaration of a class?Why is the C++ STL is so heavily based on templates? (and not on *interfaces*)Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition
What is the German equivalent of the proverb 水清ければ魚棲まず (if the water is clear, fish won't live there)?
To find islands of 1 and 0 in matrix
Composing fill in the blanks
Should I intervene when a colleague in a different department makes students run laps as part of their grade?
Why is softmax function used to calculate probabilities although we can divide each value by the sum of the vector?
What are the closest international airports in different countries?
How well would the Moon protect the Earth from an Asteroid?
How did the SysRq key get onto modern keyboards if it's rarely used?
What language is Raven using for her attack in the new 52?
Who said "one can be a powerful king with a very small sceptre"?
Why is it considered acid rain with pH <5.6?
How do I make my photos have more impact?
If you inherit a Roth 401(k), is it taxed?
Can Lightning Lure be used to knock out a creature like a magical Taser?
Why would a personal invisible shield be necessary?
Self-deportation of American Citizens from US
GNU sort stable sort when sort does not know sort order
Is it okay for me to decline a project on ethical grounds?
Unknown indication below upper stave
Is there a word to describe someone who is, or the state of being, content with hanging around others without interacting with them?
Shouldn't there be "us" instead of "our" in this sentence?
Is The Venice Syndrome documentary cover photo real?
Why would anyone ever invest in a cash-only etf?
Why force the nose of 737 Max down in the first place?
Declare new function inside a class using a template
What is the difference between old style and new style classes in Python?Are static class variables possible in Python?Use 'class' or 'typename' for template parameters?When to use static classes in C#Is it possible to write a template to check for a function's existence?Why can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?Meaning of 'const' last in a function declaration of a class?Why is the C++ STL is so heavily based on templates? (and not on *interfaces*)Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm still learning about templates. I'm not sure whether you can declare/(automatically define) a function inside a class (method) using a template. That is, I have a function template defined like this, for example:
template<typename T>
T getT()
T result;
return result;
And a class, where I want the "new function" to be created, based on the template, like this:
class World
public:
World();
~World();
getT<int>; //"Magically" create new function from the template (return type 'int')
What I actually want is to have only a method with the specific given type in World. That means when I want to "magically" create a method based on the template, I want to sort of copy-paste the template function into the class but with the given type.
For example:
class World
public:
World();
~World();
//The magically created function with T equal to int
int getT()
int result;
return result;
Then of course I expect to be able to call the function:
int main()
World world; //Create world object
world.getT<int>; //Call the function
return 0;
Even though here I say I would call it with getT<int>, it could be only getT() (if it is a perfect copy-paste of the template function).
c++ class templates
add a comment |
I'm still learning about templates. I'm not sure whether you can declare/(automatically define) a function inside a class (method) using a template. That is, I have a function template defined like this, for example:
template<typename T>
T getT()
T result;
return result;
And a class, where I want the "new function" to be created, based on the template, like this:
class World
public:
World();
~World();
getT<int>; //"Magically" create new function from the template (return type 'int')
What I actually want is to have only a method with the specific given type in World. That means when I want to "magically" create a method based on the template, I want to sort of copy-paste the template function into the class but with the given type.
For example:
class World
public:
World();
~World();
//The magically created function with T equal to int
int getT()
int result;
return result;
Then of course I expect to be able to call the function:
int main()
World world; //Create world object
world.getT<int>; //Call the function
return 0;
Even though here I say I would call it with getT<int>, it could be only getT() (if it is a perfect copy-paste of the template function).
c++ class templates
YourgetTreturns a reference to a local variable, which is a no-no.
– 1201ProgramAlarm
Mar 26 at 21:03
1
There are many ways to implement something like that (using traits, CRTP, Mixins ...). But the choice depends on where you want to put the complexity and/or the specialisation. Can you be more specific ?
– Michael Doubez
Mar 26 at 21:50
@MichaelDoubez I literally just want to "copy-paste" the template function into the class but with desired type. That's it. I said I would call it by world.getT<int>, but being able to call it by getT() would be even better.
– Nyck
Mar 26 at 22:09
1
@Nyck The only doable copy paste is by using macro (sic). Rule of thumb it to put as much code as possible in a free function template and use a macro to define code that call it (in your case#define DEFINE_GET_T(T) T getT()return ::getT<T>();) and thenDEFINE_GET_T(int)in the class. Otherwise, for such a simple/general case, the solution of inheriting a helper is enough (R Sahu answer). Another solution (that I prefer it to keep it as free function). I'll post and answer about that.
– Michael Doubez
Mar 26 at 22:36
add a comment |
I'm still learning about templates. I'm not sure whether you can declare/(automatically define) a function inside a class (method) using a template. That is, I have a function template defined like this, for example:
template<typename T>
T getT()
T result;
return result;
And a class, where I want the "new function" to be created, based on the template, like this:
class World
public:
World();
~World();
getT<int>; //"Magically" create new function from the template (return type 'int')
What I actually want is to have only a method with the specific given type in World. That means when I want to "magically" create a method based on the template, I want to sort of copy-paste the template function into the class but with the given type.
For example:
class World
public:
World();
~World();
//The magically created function with T equal to int
int getT()
int result;
return result;
Then of course I expect to be able to call the function:
int main()
World world; //Create world object
world.getT<int>; //Call the function
return 0;
Even though here I say I would call it with getT<int>, it could be only getT() (if it is a perfect copy-paste of the template function).
c++ class templates
I'm still learning about templates. I'm not sure whether you can declare/(automatically define) a function inside a class (method) using a template. That is, I have a function template defined like this, for example:
template<typename T>
T getT()
T result;
return result;
And a class, where I want the "new function" to be created, based on the template, like this:
class World
public:
World();
~World();
getT<int>; //"Magically" create new function from the template (return type 'int')
What I actually want is to have only a method with the specific given type in World. That means when I want to "magically" create a method based on the template, I want to sort of copy-paste the template function into the class but with the given type.
For example:
class World
public:
World();
~World();
//The magically created function with T equal to int
int getT()
int result;
return result;
Then of course I expect to be able to call the function:
int main()
World world; //Create world object
world.getT<int>; //Call the function
return 0;
Even though here I say I would call it with getT<int>, it could be only getT() (if it is a perfect copy-paste of the template function).
c++ class templates
c++ class templates
edited Mar 26 at 22:13
Nyck
asked Mar 26 at 20:33
NyckNyck
821 silver badge8 bronze badges
821 silver badge8 bronze badges
YourgetTreturns a reference to a local variable, which is a no-no.
– 1201ProgramAlarm
Mar 26 at 21:03
1
There are many ways to implement something like that (using traits, CRTP, Mixins ...). But the choice depends on where you want to put the complexity and/or the specialisation. Can you be more specific ?
– Michael Doubez
Mar 26 at 21:50
@MichaelDoubez I literally just want to "copy-paste" the template function into the class but with desired type. That's it. I said I would call it by world.getT<int>, but being able to call it by getT() would be even better.
– Nyck
Mar 26 at 22:09
1
@Nyck The only doable copy paste is by using macro (sic). Rule of thumb it to put as much code as possible in a free function template and use a macro to define code that call it (in your case#define DEFINE_GET_T(T) T getT()return ::getT<T>();) and thenDEFINE_GET_T(int)in the class. Otherwise, for such a simple/general case, the solution of inheriting a helper is enough (R Sahu answer). Another solution (that I prefer it to keep it as free function). I'll post and answer about that.
– Michael Doubez
Mar 26 at 22:36
add a comment |
YourgetTreturns a reference to a local variable, which is a no-no.
– 1201ProgramAlarm
Mar 26 at 21:03
1
There are many ways to implement something like that (using traits, CRTP, Mixins ...). But the choice depends on where you want to put the complexity and/or the specialisation. Can you be more specific ?
– Michael Doubez
Mar 26 at 21:50
@MichaelDoubez I literally just want to "copy-paste" the template function into the class but with desired type. That's it. I said I would call it by world.getT<int>, but being able to call it by getT() would be even better.
– Nyck
Mar 26 at 22:09
1
@Nyck The only doable copy paste is by using macro (sic). Rule of thumb it to put as much code as possible in a free function template and use a macro to define code that call it (in your case#define DEFINE_GET_T(T) T getT()return ::getT<T>();) and thenDEFINE_GET_T(int)in the class. Otherwise, for such a simple/general case, the solution of inheriting a helper is enough (R Sahu answer). Another solution (that I prefer it to keep it as free function). I'll post and answer about that.
– Michael Doubez
Mar 26 at 22:36
Your
getT returns a reference to a local variable, which is a no-no.– 1201ProgramAlarm
Mar 26 at 21:03
Your
getT returns a reference to a local variable, which is a no-no.– 1201ProgramAlarm
Mar 26 at 21:03
1
1
There are many ways to implement something like that (using traits, CRTP, Mixins ...). But the choice depends on where you want to put the complexity and/or the specialisation. Can you be more specific ?
– Michael Doubez
Mar 26 at 21:50
There are many ways to implement something like that (using traits, CRTP, Mixins ...). But the choice depends on where you want to put the complexity and/or the specialisation. Can you be more specific ?
– Michael Doubez
Mar 26 at 21:50
@MichaelDoubez I literally just want to "copy-paste" the template function into the class but with desired type. That's it. I said I would call it by world.getT<int>, but being able to call it by getT() would be even better.
– Nyck
Mar 26 at 22:09
@MichaelDoubez I literally just want to "copy-paste" the template function into the class but with desired type. That's it. I said I would call it by world.getT<int>, but being able to call it by getT() would be even better.
– Nyck
Mar 26 at 22:09
1
1
@Nyck The only doable copy paste is by using macro (sic). Rule of thumb it to put as much code as possible in a free function template and use a macro to define code that call it (in your case
#define DEFINE_GET_T(T) T getT()return ::getT<T>(); ) and then DEFINE_GET_T(int) in the class. Otherwise, for such a simple/general case, the solution of inheriting a helper is enough (R Sahu answer). Another solution (that I prefer it to keep it as free function). I'll post and answer about that.– Michael Doubez
Mar 26 at 22:36
@Nyck The only doable copy paste is by using macro (sic). Rule of thumb it to put as much code as possible in a free function template and use a macro to define code that call it (in your case
#define DEFINE_GET_T(T) T getT()return ::getT<T>(); ) and then DEFINE_GET_T(int) in the class. Otherwise, for such a simple/general case, the solution of inheriting a helper is enough (R Sahu answer). Another solution (that I prefer it to keep it as free function). I'll post and answer about that.– Michael Doubez
Mar 26 at 22:36
add a comment |
2 Answers
2
active
oldest
votes
Be carfull
template<typename T>
T& getT()
T result;
return result;
Will return a reference to a temporary. Please do
template<typename T>
T getT()
T result;
return result;
without "&"
And if it's just for get a specific member, you can use std::tuple.
https://en.cppreference.com/w/cpp/utility/tuple/get
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
add a comment |
getT<int>; //"Magically" create new function from the template (return type 'int')
I don't think that will work.
It appears as though you would like to be able to use templates like macro expansion. Unfortunately, they are very different things and templates don't work like macro expansion.
However, you can use something like the following:
template<typename T>
struct GetHelper
T get()
return T;
;
class World : private GetHelper<int>,
private GetHelper<double>
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(this)->get();
;
Now you can use:
World w;
int a = w.get<int>();
double b = w.get<double>();
You may also hide GetHelper as a private type of World as:
class World
private:
template<typename T>
struct GetHelper
T get()
return T;
;
struct Data : GetHelper<int>,
GetHelper<double>;
Data data;
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(&data)->get();
;
1
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simpletemplate <typename T> T getT() return T;method inside the class World? Here is a simple example cpp.sh/8hmeq
– cplusplusrat
Mar 26 at 20:50
1
@cplusplusrat, It will absolutely work. What I was saying isget<int>();as a member function won't work. I think the OP thinks that templates can be used like macro expansion.
– R Sahu
Mar 26 at 20:58
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
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%2f55365782%2fdeclare-new-function-inside-a-class-using-a-template%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
Be carfull
template<typename T>
T& getT()
T result;
return result;
Will return a reference to a temporary. Please do
template<typename T>
T getT()
T result;
return result;
without "&"
And if it's just for get a specific member, you can use std::tuple.
https://en.cppreference.com/w/cpp/utility/tuple/get
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
add a comment |
Be carfull
template<typename T>
T& getT()
T result;
return result;
Will return a reference to a temporary. Please do
template<typename T>
T getT()
T result;
return result;
without "&"
And if it's just for get a specific member, you can use std::tuple.
https://en.cppreference.com/w/cpp/utility/tuple/get
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
add a comment |
Be carfull
template<typename T>
T& getT()
T result;
return result;
Will return a reference to a temporary. Please do
template<typename T>
T getT()
T result;
return result;
without "&"
And if it's just for get a specific member, you can use std::tuple.
https://en.cppreference.com/w/cpp/utility/tuple/get
Be carfull
template<typename T>
T& getT()
T result;
return result;
Will return a reference to a temporary. Please do
template<typename T>
T getT()
T result;
return result;
without "&"
And if it's just for get a specific member, you can use std::tuple.
https://en.cppreference.com/w/cpp/utility/tuple/get
edited Mar 26 at 21:33
answered Mar 26 at 21:29
Martin MorterolMartin Morterol
1,0461 gold badge2 silver badges8 bronze badges
1,0461 gold badge2 silver badges8 bronze badges
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
add a comment |
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
No need to add an answer to tell me that. But thank you, I had seen @1201ProgramAlarm 's comment, but forgot about it. Will edit.
– Nyck
Mar 26 at 21:32
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
my bad didn't see @1201ProgramAlarm 's comment
– Martin Morterol
Mar 26 at 21:34
add a comment |
getT<int>; //"Magically" create new function from the template (return type 'int')
I don't think that will work.
It appears as though you would like to be able to use templates like macro expansion. Unfortunately, they are very different things and templates don't work like macro expansion.
However, you can use something like the following:
template<typename T>
struct GetHelper
T get()
return T;
;
class World : private GetHelper<int>,
private GetHelper<double>
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(this)->get();
;
Now you can use:
World w;
int a = w.get<int>();
double b = w.get<double>();
You may also hide GetHelper as a private type of World as:
class World
private:
template<typename T>
struct GetHelper
T get()
return T;
;
struct Data : GetHelper<int>,
GetHelper<double>;
Data data;
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(&data)->get();
;
1
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simpletemplate <typename T> T getT() return T;method inside the class World? Here is a simple example cpp.sh/8hmeq
– cplusplusrat
Mar 26 at 20:50
1
@cplusplusrat, It will absolutely work. What I was saying isget<int>();as a member function won't work. I think the OP thinks that templates can be used like macro expansion.
– R Sahu
Mar 26 at 20:58
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
add a comment |
getT<int>; //"Magically" create new function from the template (return type 'int')
I don't think that will work.
It appears as though you would like to be able to use templates like macro expansion. Unfortunately, they are very different things and templates don't work like macro expansion.
However, you can use something like the following:
template<typename T>
struct GetHelper
T get()
return T;
;
class World : private GetHelper<int>,
private GetHelper<double>
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(this)->get();
;
Now you can use:
World w;
int a = w.get<int>();
double b = w.get<double>();
You may also hide GetHelper as a private type of World as:
class World
private:
template<typename T>
struct GetHelper
T get()
return T;
;
struct Data : GetHelper<int>,
GetHelper<double>;
Data data;
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(&data)->get();
;
1
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simpletemplate <typename T> T getT() return T;method inside the class World? Here is a simple example cpp.sh/8hmeq
– cplusplusrat
Mar 26 at 20:50
1
@cplusplusrat, It will absolutely work. What I was saying isget<int>();as a member function won't work. I think the OP thinks that templates can be used like macro expansion.
– R Sahu
Mar 26 at 20:58
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
add a comment |
getT<int>; //"Magically" create new function from the template (return type 'int')
I don't think that will work.
It appears as though you would like to be able to use templates like macro expansion. Unfortunately, they are very different things and templates don't work like macro expansion.
However, you can use something like the following:
template<typename T>
struct GetHelper
T get()
return T;
;
class World : private GetHelper<int>,
private GetHelper<double>
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(this)->get();
;
Now you can use:
World w;
int a = w.get<int>();
double b = w.get<double>();
You may also hide GetHelper as a private type of World as:
class World
private:
template<typename T>
struct GetHelper
T get()
return T;
;
struct Data : GetHelper<int>,
GetHelper<double>;
Data data;
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(&data)->get();
;
getT<int>; //"Magically" create new function from the template (return type 'int')
I don't think that will work.
It appears as though you would like to be able to use templates like macro expansion. Unfortunately, they are very different things and templates don't work like macro expansion.
However, you can use something like the following:
template<typename T>
struct GetHelper
T get()
return T;
;
class World : private GetHelper<int>,
private GetHelper<double>
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(this)->get();
;
Now you can use:
World w;
int a = w.get<int>();
double b = w.get<double>();
You may also hide GetHelper as a private type of World as:
class World
private:
template<typename T>
struct GetHelper
T get()
return T;
;
struct Data : GetHelper<int>,
GetHelper<double>;
Data data;
public:
World()
~World()
template <typename T>
get()
return static_cast<GetHelper<T>*>(&data)->get();
;
edited Mar 26 at 21:58
answered Mar 26 at 20:44
R SahuR Sahu
175k12 gold badges103 silver badges201 bronze badges
175k12 gold badges103 silver badges201 bronze badges
1
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simpletemplate <typename T> T getT() return T;method inside the class World? Here is a simple example cpp.sh/8hmeq
– cplusplusrat
Mar 26 at 20:50
1
@cplusplusrat, It will absolutely work. What I was saying isget<int>();as a member function won't work. I think the OP thinks that templates can be used like macro expansion.
– R Sahu
Mar 26 at 20:58
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
add a comment |
1
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simpletemplate <typename T> T getT() return T;method inside the class World? Here is a simple example cpp.sh/8hmeq
– cplusplusrat
Mar 26 at 20:50
1
@cplusplusrat, It will absolutely work. What I was saying isget<int>();as a member function won't work. I think the OP thinks that templates can be used like macro expansion.
– R Sahu
Mar 26 at 20:58
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
1
1
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simple
template <typename T> T getT() return T; method inside the class World? Here is a simple example cpp.sh/8hmeq– cplusplusrat
Mar 26 at 20:50
I am sure I misunderstood, but why won't a template method in a class work? Why can't one have a simple
template <typename T> T getT() return T; method inside the class World? Here is a simple example cpp.sh/8hmeq– cplusplusrat
Mar 26 at 20:50
1
1
@cplusplusrat, It will absolutely work. What I was saying is
get<int>(); as a member function won't work. I think the OP thinks that templates can be used like macro expansion.– R Sahu
Mar 26 at 20:58
@cplusplusrat, It will absolutely work. What I was saying is
get<int>(); as a member function won't work. I think the OP thinks that templates can be used like macro expansion.– R Sahu
Mar 26 at 20:58
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
Right, re-read the post. Yes, some sort of template specialization would be required to invoke specific method based on template parameters.
– cplusplusrat
Mar 26 at 21:00
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%2f55365782%2fdeclare-new-function-inside-a-class-using-a-template%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
Your
getTreturns a reference to a local variable, which is a no-no.– 1201ProgramAlarm
Mar 26 at 21:03
1
There are many ways to implement something like that (using traits, CRTP, Mixins ...). But the choice depends on where you want to put the complexity and/or the specialisation. Can you be more specific ?
– Michael Doubez
Mar 26 at 21:50
@MichaelDoubez I literally just want to "copy-paste" the template function into the class but with desired type. That's it. I said I would call it by world.getT<int>, but being able to call it by getT() would be even better.
– Nyck
Mar 26 at 22:09
1
@Nyck The only doable copy paste is by using macro (sic). Rule of thumb it to put as much code as possible in a free function template and use a macro to define code that call it (in your case
#define DEFINE_GET_T(T) T getT()return ::getT<T>();) and thenDEFINE_GET_T(int)in the class. Otherwise, for such a simple/general case, the solution of inheriting a helper is enough (R Sahu answer). Another solution (that I prefer it to keep it as free function). I'll post and answer about that.– Michael Doubez
Mar 26 at 22:36