Well, if nothing else, I'm learning lots about the corners of the
language!
I've got the following testcase:
#include
David wrote:
template<typename Type> void print_handle(typename Handle<Type>::type ptr) { std::cout << "Value is " << *ptr << std::endl; }
For such a function "Type" can't be deduced. There is a certain set of rules which specify when types can be deduced, specified in the standard, but I can recommend the book "C++ Templates" by Vandervoorde and Josuttis for a nicer reading on this. It also has some nice examples about really dark corners of the language. It's quite obvious, if you think of it: The compiler would have to insert all possibilities for "Type" and include all those in the overload resolution if you would want to make the language work this way.
What's going on here? Is there a way to make this work without having to explicitly specify the type to print_handle?
Why is there a need? Presumably you're potential surrogate for shared_ptr will also support operator *, and that's all you use in your function. Jens
Jens Theisen wrote:
It's quite obvious, if you think of it: The compiler would have to insert all possibilities for "Type" and include all those in the overload resolution if you would want to make the language work this way.
Ok, I see now. I keep forgetting about that pesky possibility of Handle specializations. :-/
What's going on here? Is there a way to make this work without having to explicitly specify the type to print_handle?
Why is there a need? Presumably you're potential surrogate for shared_ptr will also support operator *, and that's all you use in your function.
Why is there a need to do what? I'm not following you here. I've gone through and specified Type everywhere in my application. It's ugly but it works. Would the upcoming template typedef feature avoid the need to specify Type in the call? template<typename Base> typedef boost::shared_ptr<Base> Handle<Base>; template<typename Type> void print_handle(Handle<Type> ptr); -Dave
David wrote:
Why is there a need? Presumably you're potential surrogate for shared_ptr will also support operator *, and that's all you use in your function.
Why is there a need to do what? I'm not following you here.
You can always do template< typename Pointer > void print_handle(Pointer const& ptr) { std::cout << *ptr << std::endl; } which will work on any smart pointer. The drawbacks are more weird error messages and less precise overload resolution since the above matches on any type, not only pointers.
Would the upcoming template typedef feature avoid the need to specify Type in the call?
I doubt it, though I haven't read the proposal. I'm pretty it's about an abbreviation for traits classes so it's not going to introduce something you don't have. Jens
participants (2)
-
David A. Greene
-
jth01@arcor.de