Topic: Template expansion during function overload resolution in C++0X


Author: Jesse Perla <jesseperla@gmail.com>
Date: Thu, 10 Jun 2010 14:06:34 CST
Raw View
I encountered a "bug" in a few compilers when doing some meta-
programming to choose return types.   On Intel 11.1 and MSVC10 the
compiler was doing a template expansion of the return types even for
overloads that don't match.  Here is a minimalist example of the
problem.
///////////////////
 template <class T> struct Z {
   typedef T::x type;
 };
 template <class T> Z<T>::type f(void *, T);
 template <class T> void f(int, T);
 struct A {} a;
 int main() {
   f(1, a);  // Works on GCC,
              // Fails on pre-fixed Intel 11.1 and MSVC10
              // because Z template expanded for unused overload
 }
/////////////////
Intel has described  a fix in:
http://software.intel.com/en-us/forums/showpost.php?p=120228

Microsoft has said that this is by design according to the latest C+
+0X standard:
https://connect.microsoft.com/VisualStudio/feedback/details/560886/bug-in-template-expansion-during-function-overload-resolution?wa=wsignin1.0#tabs

Microsoft's reasoning is:  This is a grey area that the C++03 standard
does not address; however, the most current draft (n3090) of the C++0x
standard (14.9.2/8) states the following:
"Only invalid types and expressions in the immediate context of the
function type and its template parameter types can result in a
deduction failure." Therefore, this behavior is by-design

Is this a correct interpretation of the standard?  Can a bug to the
standard be submitted to cover this case?  I think that this pattern
can come up all over the place in metaprogramming.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]