More C++ Idioms/Enable if

From Wikibooks, open books for an open world
Jump to: navigation, search

Contents

[edit] Intent

Sometimes a member function of a class template (possibly one of an overloaded set) only makes sense for certain template parameters. The enable_if idiom uses SFINAE to eliminate unwanted functions before overload resolution.

[edit] Also Known As

[edit] Motivation

[edit] Solution and Sample Code

 

[edit] Known Uses

One use of enable_if is in implementing requirements such as that on the (size_type, value_type) constructor of a standard container, which has special behavior if the value_type is an integral type. More primitive solutions exist, but enable_if is elegant in this context.

[edit] Related Idioms

SFINAE, metafunctions.

[edit] References

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export