[Original] in-depth understanding of sfinae (matching failure is not an error)

Source: Internet
Author: User

[Author: Dong Bo blog: http://84638372.qzone.qq.com /]
I recently heard of this concept. Its source is extended STL, Volume 1: collections and iterators, in chapter 13th. Sfinae, that is, substitution failure is not an error! It can be understood that the matching failure is not an error. More strictly speaking, the parameter matching failure is not a compilation error. Looking at these explanations, I think that, apart from a few C ++ experts, I will be confused. Of course, I am not an expert, so I am confused.
I am Google with an attitude that I don't know, but I don't know much about it. I found some information on Wikipedia, but I didn't quite understand it after reading it. Of course, I don't know if I don't want to explore it. This is against my code of conduct. Later I found something similar in other books and I will share it with you. See the following code:
# Include <iostream>
Using namespace STD;
Void print (INT inum)
{
Cout <"int print (INT)" <Endl;
}
Template <typename _ ty>
Void print (_ ty TT)
{
Typename _ TY: value_type vt_someval;
Cout <"template <typename _ ty>" <Endl;
}
Int main ()
{
Short sinum = 10;
Print (sinum );

Return 0;
}
Can this code be compiled? Practice has proved that this Code cannot be compiled, for example, my error under Visual Studio 2008 SP1:
1> E:/Documents documents/Visual Studio 2008/projects/Baidu/main. CPP (13): Error c2825: '_ ty': When followed by ":", it must be a class or namespace.
1> E:/Documents documents/Visual Studio 2008/projects/Baidu/main. CPP (20): see references to instantiate "Void print <short> (_ ty)" for the function template being compiled.
1>
1> [
1> _ ty = short
1>]
1> E:/Documents documents/Visual Studio 2008/projects/Baidu/Main. cpp (13): Error c2039: "value_type": Not a member of "'Global namespace '"
1> E:/Documents documents/Visual Studio 2008/projects/Baidu/main. CPP (13): Error c2146: syntax error: Missing ";" (before the identifier "vt_someval)
1> E:/Documents ents/Visual Studio 2008/projects/Baidu/Main. cpp (13): Error c2065: "vt_someval": Undeclared identifier
How can this compilation error be solved? There are many methods, for example, we can specialize in the Short Type:
Template <>
Void print <short> (short st)
{
Cout <"Short ST" <Endl;
}
Practice has proved that this can solve this problem, but we are discussing sfinae here. See the following code:
# Include <iostream>
Using namespace STD;
Void print (INT inum)
{
Cout <"int print (INT)" <Endl;
}
Template <typename _ ty>
Void print (_ ty TT, typename _ TY: value_type * pvt_dummy = NULL)
{
Typename _ TY: value_type vt_someval;
Cout <"template <typename _ ty>" <Endl;
}
Int main ()
{
Short sinum = 10;
Print (sinum );

Return 0;
}
If your compiler is correct, this program can be compiled. Why does the compiler choose to upgrade short to int to execute the first print instead of using short to instantiate the print template? This is a feature of sfinae and C ++.
Sfinae was first proposed by daveed vandevorde and niclai josutis, which means it would rather not generate a compile-time error for problematic types without considering function overloading. If there is a value_type nested type in the parameter type, it is part of the overload resolution set.
A little dizzy? OMG, in fact, is also dizzy. In other words, when the compiler identifies a function template, if there is a special feature that will cause a compilation error (that is, a compilation failure occurs), as long as there are other options available, then, ignore this special error and choose another option. For example:
Class Test
{
};
Int main ()
{
Test;
Print ();

Return 0;
}
The compiler gives the following error:
Error c2664: "print": parameter 1 cannot be converted from "test" to "int"
Instead of issues related to template specialization.
This is a very important feature of C ++. Without this feature, it will damage a lot of early code (because there was no template at the time) and produce a lot of hard-to-understand code.
Looking back, why does the value_type take a part of the parameter or the return value compiler realize that the type we provide is not suitable for semantics and the original code cannot find it? This depends on the template instantiation process. If both the return value and parameters can be matched, this instantiation is equivalent to success. At this moment, it indicates that the compiler has selected template specialization instead of other options; when the constraint is in the parameter or return value, the template parameter matching will fail, resulting in a compilation error, at this time, the compiler will follow the sfinae principle to see if there are other options.

Thanks to the rapid development of generic programming and template metaprogramming, we provide a lot of good solutions in the boost library enable_if, MPL, type_traits. If you are interested, you can read these books "C ++ template metaprogramming" (English version: "C ++. template. metaprogramming, and introduction to boost library, beyond the C ++ standard library (English version: Beyond the C ++ standard library: An Introduction to boost). of course, there are also boost documents.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.