Substitution failure is not an error

Substitution failure is not an error

Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters is not in itself an error. The acronym SFINAE was first introduced by David Vandevoorde to describe related programming techniques. [cite book | last=Vandevoorde | first=David | coauthors=Nicolai M. Josuttis | title=C++ Templates: The Complete Guide | publisher=Addison-Wesley Professional | year=2002 | id=ISBN 0-201-73484-2]

Specifically, when creating a candidate set for overload resolution, some (or all) candidates of that set may be the result of substituting template parameters with deduced template arguments. If during substitution an error occurs, the potential candidate is just dropped from the candidate set (instead of making the program erroneous outright) provided the substitution error is recognized for that treatment by the C++ standard. [International Organization for Standardization. "ISO/IEC 14882:2003, Programming languages — C++", § 14.8.2.] If one or more candidates remain and overload resolution succeeds, the program may be well-formed.

The following example illustrates a basic instance of SFINAE:

struct Test { typedef int Type;};

template < typename T > void f( typename T::Type ) {} // definition #1

template < typename T > void f( T ) {} // definition #2

f< Test > ( 10 ); //call #1

f< int > ( 10 ); //call #2 without error thanks to SFINAE

Here, with SFINAE, call #2 will not produce an error because definition #1 is just skipped by the compiler.

Although SFINAE as a language behavior was designed to avoid programs becoming ill-formed because unrelated template declarations were visible (e.g., through the inclusion of a header file), it was found to be a useful means for compile-time introspection. Specifically, it allows a template to determine certain properties of its template arguments at instantiation time.

In C++0x, in most cases SFINAE overloading can be eliminated by concepts [ [http://www.devx.com/SpecialReports/Article/38813/0/page/3] An Interview with Bjarne Stroustrup]

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • C++ — The C++ Programming Language, written by its architect, is the seminal book on the language. Paradigm(s) Multi paradigm:[1] procedural …   Wikipedia

  • Typographical error — Typo redirects here. For other uses, see Typo (disambiguation). For the Wikipedia Typo Team, see Wikipedia:Typo. A typographical omission of the word not in the sentence Thou shalt not commit adultery. This printing of the Bible has become known… …   Wikipedia

  • C++ — Apparu en 1983[1] (dernière révision en 2008) …   Wikipédia en Français

  • cryptology — cryptologist, n. cryptologic /krip tl oj ik/, cryptological, adj. /krip tol euh jee/, n. 1. cryptography. 2. the science and study of cryptanalysis and cryptography. [1635 45; < NL cryptologia. See CRYPTO , LOGY] * * * Introduction …   Universalium

  • David Kaplan (philosopher) — For other people of the same name, see David Kaplan (disambiguation). David Benjamin Kaplan (born 1933) is an American philosopher and logician teaching at UCLA. His philosophical work focuses on logic, philosophical logic, modality, philosophy… …   Wikipedia

  • Opioid — Endogenous opioid peptides Skeletal molecular images Adrenorphin Amidorphin Casomorphin …   Wikipedia

  • Hysteresis — Not to be confused with Hysteria. Fig. 1. Electric displacement field D of a ferroelectric material as the electric field E is first decreased, then increased. The curves form a hysteresis loop. Hysteresis is the dependence of a system not… …   Wikipedia

  • Sharia — Not to be confused with Shahriyār. This article is part of the series …   Wikipedia

  • NATO phonetic alphabet — Not to be confused with International Phonetic Alphabet. FAA radiotelephony alphabet and Morse code chart The NATO phonetic alphabet, more accurately known as the NATO spelling alphabet and also called the ICAO phonetic or spelling alphabet, the… …   Wikipedia

  • Japanese cryptology from the 1500s to Meiji — The cipher system that Uesugi used is basically a simple substitution usually known as a Polybius square or “checkerboard.” The i ro ha alphabet contains forty eight letters, so a seven by seven square is used, with one of the cells left blank.… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”