Template Argument Deduction Order [C++]
I have a template function that looks like this
template<typename T, T J>
void Func()
{
...
}
Problem with this is, it means I must always specify the type:
Func<int,5>
When I really want to do
Func<5>
If I try however to write:
template<T J, typename T>
It obviously complains about T not existing at that point.
Is there anyway I can not have to specify the type, because the types I am using are function pointers and they have long to write out typenames.
Only way of doing it would be to pass T as a parameter to the function, not a template:
There's no "templated templates" as you're suggesting [grin]
template<typename T>void Func(T val){}Func(5);
There's no "templated templates" as you're suggesting [grin]
But it does work, this successfully compiles:
That wasn't my question, mine was in that example is their anyway to get it to deduce the first template argument by the second one. Something like:
Function<,4>() instead of having to write Function<int,4>()
template<typename T, T J>T Function(){ return J;}
That wasn't my question, mine was in that example is their anyway to get it to deduce the first template argument by the second one. Something like:
Function<,4>() instead of having to write Function<int,4>()
As far as I know, template type deduction only works with function arguments.
I think the only way would be to do it with a macro, and using BOOST_TYPEOF, GCC's typeof or C++0x's decltype.
I think the only way would be to do it with a macro, and using BOOST_TYPEOF, GCC's typeof or C++0x's decltype.
#include <boost/typeof/typeof.hpp>#define ADD_TYPE_OF(n) BOOST_TYPEOF(n), ntemplate<typename T, T J>void Func(){ //...}void foo(int) {}int main(){ Func<ADD_TYPE_OF(5)>(); Func<ADD_TYPE_OF(&foo)>();}
Ok but my primary concern in that the argument J is going to be a C++ function so the type would be something like int(*)(int,char), will BOOST_TYPEOF work with that because I tried and it just complained that J was unexpected, where writing int(*)(int,char) worked perfectly.
My example also includes function pointers. Perhaps you omitted the address-of: those are generally optional for free function pointers, but here it is apparently needed.
I think the best you'll be able to do is make liberal use of typedef to shorten your type names. You should be using typedef anyways; if your function pointer signature changes, for instance, you don't want to have to dig through all of your code and change every single place where you've used that signature. So with typedefs you have exactly one place that needs to be updated should you need to make a change, and as a nice bonus the type's name can be as long or short as you like.
Ok I'm glad that it works for function pointers, I'll try it. Otherwise I would use typedefs but I actually need to run this on a bunch of native windows functions so it's easier this way then writing a typedef for each of the dozen I need to run it on.
AFAIK only integral arguments can be used as template parameters anyway. So the usefulness of templating on the type for a templated value is rather limited, since there are only a few types it could possibly have anyway. I'd just expect int (or size_t, depending on what it represents) and be done with it.
IIRC the legal types are integrals and bool, which is used heavily in template metaprogramming, and might or might not be considered an integral type depending on how picky you want to get [wink]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement