C++11 Tidbits: access control under SFINAE conditions
Posted
by Paolo Carlini
on Oracle Blogs
See other posts from Oracle Blogs
or by Paolo Carlini
Published on Mon, 17 Sep 2012 15:19:48 +0000
Indexed on
2012/09/17
21:46 UTC
Read the original article
Hit count: 329
/Oracle
Lately I have been spending quite a bit of time on the SFINAE ("Substitution failure is not an error") features of C++, fixing and tweaking various bits of the GCC implementation.
An important missing piece was the implementation of the resolution of DR 1170 which, in a nutshell, mandates that access checking is done as part of the substitution process. Consider:
class C { typedef int type; }; template <class T, class = typename T::type> auto f(int) -> char; template <class> auto f(...) -> char (&)[2]; static_assert (sizeof(f<C>(0)) == 2, "Ouch");
According to the resolution, the static_assert should not fire, and the snippet should compile successfully. The reason being that the first f overload must be removed from the candidate set because C::type is private to C. On the other hand, before the resolution of DR 1170, the expected behavior was for the first overload to remain in the candidate set, win over the second one, to eventually lead to an access control error (*).
GCC mainline (would be 4.8) finally implements the DR, thus benefiting the many modern programming techniques heavily exploiting SFINAE, among which certainly the GNU C++ runtime library itself, which relies on it for the internals of <type_traits> and in several other places.
Note that the resolution of the DR is active even in C++98 mode, not just in C++11 mode, because it turned out that the traditional behavior, as implemented in GCC, wasn't fully consistent in all the possible circumstances.
(*) In practice, GCC didn't really implement this, the static_assert triggered instead.