Re: [Boost-users] Tag dispatching

----- Original Message ----- From: "Ovanes Markarian"
To: boost-users@lists.boost.org Subject: Re: [Boost-users] Tag dispatching Date: Wed, 4 Mar 2009 14:12:09 +0100 Hicham,
please see my comments below:
On Wed, Mar 4, 2009 at 1:54 PM, Hicham Mouline
wrote: Hello,
I have a question about tag dispatching (for e.g. the std::advance() function in the std lib, as described for instance in http://www.boost.org/community/generic_programming.html#tag_dispatching)
1. The call to detail::advance_dispatch(i, n, category); I don't understand. category is a type not an object, it should be category(), right?
typename *iterator_traits<InputIterator>::iterator_category* category; detail::advance_dispatch(i, n, *category*);
here is no typedef used, so category is an instance of iterator_category. In the template context you have to prefix it with typename keyword. Using:
typename *iterator_traits<InputIterator>::iterator_category* category();
declares a function, which returns iterator_category, but does not create a class instance. ah yes of course. I misread the typename as typedef, thanks. category is an auto instance. Can it be also written also as a temporary etail::advance_dispatch(i, n, iterator_traits<InputIterator>::iterator_category() );
2. If I am correct, the tags are empty structs which I assume allows modern compilers (g++4, msvc8/9, intel) to optimize away the creation of that category object ?
This is compiler dependent. ISO C++ 03 standard requires an instance of an empty type to be at least 1 byte big. To check if that happens read the documentation to the corresponding compiler.
3. If i am still correct, I have a tag which has no members except 1 static function that returns void. Would that optimization still apply?
Applies the same as in 2. Whether the compiler prints sizeof(myType) as 1byte or something else has no bearing on whether it doesn't create an instance at runtime, right? need to read their docs (though it's quite hard to see where for g++ for e.g.)
thanks very much, regards,

On Wed, Mar 4, 2009 at 2:29 PM, Hicham Mouline
Whether the compiler prints sizeof(myType) as 1byte or something else has no bearing on whether it doesn't create an instance at runtime, right? need to read their docs (though it's quite hard to see where for g++ for e.g.)
Yes that's true. But nobody will give you a definite answer by referencing to the C++ Standard. This is a really compiler dependent issue. What you can do is inspecting the function signatures created in your compiler's binary output. If you have an overload with 2 parameters, than it means compiler applied some sort of optimization, otherwise, it will pass and instance of the object, since the function with 2 parameters is simply not there ;)
thanks very much,
You are welcome
participants (2)
-
Hicham Mouline
-
Ovanes Markarian