How to typedef template function pointer?
up vote
1
down vote
favorite
I want to typedef a function pointer that points to a template function.
class A
template <typename T>
typedef void (*FPTR)<T>();
I have tried in this way and didn't succeed. Any idea about this thing?
c++ c++11 templates function-pointers typedef
add a comment |
up vote
1
down vote
favorite
I want to typedef a function pointer that points to a template function.
class A
template <typename T>
typedef void (*FPTR)<T>();
I have tried in this way and didn't succeed. Any idea about this thing?
c++ c++11 templates function-pointers typedef
1
Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49
If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();
, or even better:using FPTR = void (*)();
). But that pointer can only point to a specific instantination of the function (e.g.FPTR ptr = foo<int>;
). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
– HolyBlackCat
Oct 28 at 10:52
@HolyBlackCat suppose I wrote downusing FPTR = void (*)();
. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types likefoo<int8>() ...
foo<int16>() ...
foo<int32>() ...
. Can I refer my pointer to these function implementations?
– Arsen Gharagyozyan
Oct 28 at 10:57
As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02
Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to typedef a function pointer that points to a template function.
class A
template <typename T>
typedef void (*FPTR)<T>();
I have tried in this way and didn't succeed. Any idea about this thing?
c++ c++11 templates function-pointers typedef
I want to typedef a function pointer that points to a template function.
class A
template <typename T>
typedef void (*FPTR)<T>();
I have tried in this way and didn't succeed. Any idea about this thing?
c++ c++11 templates function-pointers typedef
c++ c++11 templates function-pointers typedef
edited Nov 11 at 8:20
JeJo
3,7763624
3,7763624
asked Oct 28 at 10:46
Arsen Gharagyozyan
334
334
1
Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49
If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();
, or even better:using FPTR = void (*)();
). But that pointer can only point to a specific instantination of the function (e.g.FPTR ptr = foo<int>;
). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
– HolyBlackCat
Oct 28 at 10:52
@HolyBlackCat suppose I wrote downusing FPTR = void (*)();
. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types likefoo<int8>() ...
foo<int16>() ...
foo<int32>() ...
. Can I refer my pointer to these function implementations?
– Arsen Gharagyozyan
Oct 28 at 10:57
As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02
Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06
add a comment |
1
Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49
If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (typedef void (*FPTR)();
, or even better:using FPTR = void (*)();
). But that pointer can only point to a specific instantination of the function (e.g.FPTR ptr = foo<int>;
). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.
– HolyBlackCat
Oct 28 at 10:52
@HolyBlackCat suppose I wrote downusing FPTR = void (*)();
. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types likefoo<int8>() ...
foo<int16>() ...
foo<int32>() ...
. Can I refer my pointer to these function implementations?
– Arsen Gharagyozyan
Oct 28 at 10:57
As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02
Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06
1
1
Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49
Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49
If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (
typedef void (*FPTR)();
, or even better: using FPTR = void (*)();
). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;
). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.– HolyBlackCat
Oct 28 at 10:52
If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (
typedef void (*FPTR)();
, or even better: using FPTR = void (*)();
). But that pointer can only point to a specific instantination of the function (e.g. FPTR ptr = foo<int>;
). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.– HolyBlackCat
Oct 28 at 10:52
@HolyBlackCat suppose I wrote down
using FPTR = void (*)();
. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ...
foo<int16>() ...
foo<int32>() ...
. Can I refer my pointer to these function implementations?– Arsen Gharagyozyan
Oct 28 at 10:57
@HolyBlackCat suppose I wrote down
using FPTR = void (*)();
. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types like foo<int8>() ...
foo<int16>() ...
foo<int32>() ...
. Can I refer my pointer to these function implementations?– Arsen Gharagyozyan
Oct 28 at 10:57
As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02
As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02
Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06
Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.
template <typename T>
void someVoidFunction()
using fPtrType = void(*)();
int main()
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.
template <typename T, typename U>
T someFunction(U u)
template <typename T, typename U>
using fPtrType = T(*)(U);
int main()
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
add a comment |
up vote
2
down vote
Template functions produce functions. Template classes produce classes. Template variables produce variables.
Pointers can point at functions or variables. They cannot point at templates; templates have no address.
Typedef defines the type of a variable.
A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.
template <typename T>
void someVoidFunction()
using fPtrType = void(*)();
int main()
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.
template <typename T, typename U>
T someFunction(U u)
template <typename T, typename U>
using fPtrType = T(*)(U);
int main()
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
add a comment |
up vote
2
down vote
accepted
As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.
template <typename T>
void someVoidFunction()
using fPtrType = void(*)();
int main()
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.
template <typename T, typename U>
T someFunction(U u)
template <typename T, typename U>
using fPtrType = T(*)(U);
int main()
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.
template <typename T>
void someVoidFunction()
using fPtrType = void(*)();
int main()
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.
template <typename T, typename U>
T someFunction(U u)
template <typename T, typename U>
using fPtrType = T(*)(U);
int main()
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.
template <typename T>
void someVoidFunction()
using fPtrType = void(*)();
int main()
fPtrType funPtr1 = &someVoidFunction<int>;
fPtrType funPtr2 = &someVoidFunction<float>;
fPtrType funPtr3 = &someVoidFunction<std::string>;
return 0;
If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.
template <typename T, typename U>
T someFunction(U u)
template <typename T, typename U>
using fPtrType = T(*)(U);
int main()
fPtrType<int, float> funPtr1 = &someFunction<int, float>; // instance 1
fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
return 0;
answered Oct 28 at 11:09
JeJo
3,7763624
3,7763624
add a comment |
add a comment |
up vote
2
down vote
Template functions produce functions. Template classes produce classes. Template variables produce variables.
Pointers can point at functions or variables. They cannot point at templates; templates have no address.
Typedef defines the type of a variable.
A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.
add a comment |
up vote
2
down vote
Template functions produce functions. Template classes produce classes. Template variables produce variables.
Pointers can point at functions or variables. They cannot point at templates; templates have no address.
Typedef defines the type of a variable.
A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.
add a comment |
up vote
2
down vote
up vote
2
down vote
Template functions produce functions. Template classes produce classes. Template variables produce variables.
Pointers can point at functions or variables. They cannot point at templates; templates have no address.
Typedef defines the type of a variable.
A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.
Template functions produce functions. Template classes produce classes. Template variables produce variables.
Pointers can point at functions or variables. They cannot point at templates; templates have no address.
Typedef defines the type of a variable.
A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.
answered Oct 28 at 11:08
Yakk - Adam Nevraumont
179k19187367
179k19187367
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53030633%2fhow-to-typedef-template-function-pointer%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
Can you make an example demonstrating how you want to use said typedef?
– HolyBlackCat
Oct 28 at 10:49
If neither parameters types nor return type of your template function depend on template parameters, then you can use a regular function pointer (
typedef void (*FPTR)();
, or even better:using FPTR = void (*)();
). But that pointer can only point to a specific instantination of the function (e.g.FPTR ptr = foo<int>;
). It's not possible to make a function pointer point to the function template itself, rather than one of if its instantinations.– HolyBlackCat
Oct 28 at 10:52
@HolyBlackCat suppose I wrote down
using FPTR = void (*)();
. Can I refer my FPTR to any initialization of foo function? Let's say I have function that has different implementations for each kind of int types likefoo<int8>() ...
foo<int16>() ...
foo<int32>() ...
. Can I refer my pointer to these function implementations?– Arsen Gharagyozyan
Oct 28 at 10:57
As I said, if neither parameters types nor return type of your template function depend on the template parameter, then you can use a function pointer. It's not clear from your comment if that's the case or not.
– HolyBlackCat
Oct 28 at 11:02
Ok thank you. Yea it is. There's no return type or arguments depending on template parameters.
– Arsen Gharagyozyan
Oct 28 at 11:06