Questions tagged [enable-if]

`std::enable_if` is a C++ metafunction template with parameters `bool B`, `class T = void`, defined in the Standard C++ header `type_traits`. If `B` is true then the instantiating class defines a public member type `type` equal to `T`; otherwise it does not.

std::enable_if is used to conditionally remove functions and classes from overload resolution based on type traits or to provide separate function overloads and specializations for different type traits. std::enable_if can be used as an additional function argument (excluding operator overloads), as a return type (excluding constructors and destructors), or as a class templateor function template parameter.

Reference documentation is provided here

447 questions
172
votes
4 answers

Why should I avoid std::enable_if in function signatures

Scott Meyers posted content and status of his next book EC++11. He wrote that one item in the book could be "Avoid std::enable_if in function signatures". std::enable_if can be used as a function argument, as a return type or as a class template or…
hansmaad
  • 16,551
  • 7
  • 46
  • 89
38
votes
4 answers

Why does enable_if_t in template arguments complains about redefinitions?

I have the following case that works using std::enable_if : template::value>::type* = nullptr> void f() { } template
Jean-Michaël Celerier
  • 5,323
  • 3
  • 39
  • 60
34
votes
10 answers

How to write a type trait `is_container` or `is_vector`?

Is it possible to write a type trait whose value is true for all common STL structures (e.g., vector, set, map, ...)? To get started, I'd like to write a type trait that is true for a vector and false otherwise. I tried this, but it doesn't…
Frank
  • 58,417
  • 87
  • 227
  • 317
31
votes
5 answers

Why compile error with enable_if

Why this does not compile with gcc48 and clang32? #include template struct S { template typename std::enable_if::type f(T t) {return 1;}; template typename…
Leonid Volnitsky
  • 8,028
  • 5
  • 31
  • 52
31
votes
2 answers

Add/Remove data members with template parameters?

Consider the following code : template class MyClass { public: void myFunction(); template::type> void addedFunction(); protected: double myVariable; …
Vincent
  • 50,257
  • 51
  • 171
  • 339
25
votes
6 answers

Loosely coupled implicit conversion

Implicit conversion can be really useful when types are semantically equivalent. For example, imagine two libraries that implement a type identically, but in different namespaces. Or just a type that is mostly identical, except for some…
ltjax
  • 15,301
  • 2
  • 34
  • 62
24
votes
5 answers

What's the right way to fix this template resolution ambiguity?

Suppose I've written: template ::value>> void foo() { std::cout << "T is integral." << std::endl; } template void foo() { std::cout << "Any T." << std::endl; } int main() {…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
22
votes
1 answer

Where is disable_if in C++0x?

Boost has both enable_if and disable_if, but C++0x seems to be missing the latter. Why was it left out? Are there meta-programming facilities in C++0x that allow me to build disable_if in terms of enable_if? Oh, I just noticed that std::enable_if…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
22
votes
1 answer

Template specialization and enable_if problems

I am running into a problem regarding the appropriate usage of enable_if and template specialization. After modifying the example (for confidentiality reasons), here's a comparable example: I have function called "less" that checks if 1st arg is…
user855
  • 16,878
  • 34
  • 86
  • 143
20
votes
3 answers

Partial template function specialization with enable_if: make default implementation

Using C++11's enable_if I want to define several specialized implementations for a function (based on the type of the parameter, say) as well as a default implementation. What is the correct way to define it? The following example does not work as…
Bruno
  • 387
  • 2
  • 7
20
votes
1 answer

Why doesn't SFINAE (enable_if) work for member functions of a class template?

#include struct A{}; struct B{}; template struct Foo { typename std::enable_if::value>::type bar() {} typename std::enable_if::value>::type bar() …
Olumide
  • 4,925
  • 6
  • 43
  • 89
20
votes
2 answers

How Does std::enable_if work?

I just asked this question: std::numeric_limits as a Condition I understand the usage where std::enable_if will define the return type of a method conditionally causing the method to fail to compile. template typename…
Jonathan Mee
  • 35,107
  • 16
  • 95
  • 241
17
votes
3 answers

What's the correct `enable_if` constraint on perfect forwarding setter?

Herb Sutter's Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance vs. ease of writing/teaching. The 'advanced' option (providing the best performance…
bames53
  • 79,748
  • 13
  • 162
  • 229
17
votes
4 answers

How do I use std::enable_if with a self-deducing return type?

C++14 will have functions whose return type can be deduced based on the return value. auto function(){ return "hello world"; } Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom? For example, let's…
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
15
votes
2 answers

Default template argument when using std::enable_if as templ. param.: why OK with two template functions that differ only in the enable_if parameter?

In the language reference of std::enable_if at cppreference the following note is included Notes A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default…
dfrib
  • 56,823
  • 7
  • 97
  • 157
1
2 3
29 30