Using SFINAE with Multiple Template Parameters

How can I apply SFINAE when my template has multiple parameters?

When applying SFINAE to a template with multiple parameters, you can introduce additional unused template parameters for each one that needs to be constrained.

For example, let's say we have a ProcessPair template that takes two type parameters T and U, and we want to ensure that T and U both have a process() member function:

#include <type_traits>
#include <iostream>

struct SomeType {
  int Value;
  void process() {
    std::cout << "Processing " << Value << '\n';
  }
};

using std::enable_if_t, std::is_same_v,
  std::declval;

template<typename T, typename U,
  enable_if_t<is_same_v<decltype(
    declval<T>().process()), void>, int> = 0,
  enable_if_t<is_same_v<decltype(
    declval<U>().process()), void>, int> = 0
>
void ProcessPair(T a, U b) {
  a.process();
  b.process();
  std::cout << "Processed pair of objects";
}

int main() {
  ProcessPair(SomeType{1}, SomeType{2});
}
Processing 1
Processing 2
Processed pair of objects

Here we've added two unused template parameters, one for each of T and U. Each one uses std::enable_if with a condition that checks if the type has a process() member function returning void.

If either T or U doesn't satisfy the condition, a substitution failure will occur, removing this template from the overload set.

By chaining multiple unused parameters like this, we can constrain any number of the template's type parameters using SFINAE.

Using SFINAE to Control Overload Resolution

Learn how SFINAE allows templates to remove themselves from overload resolution based on their arguments, and apply it in practical examples.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Applying SFINAE to Non-Type Template Parameters
Can I use SFINAE with non-type template parameters, like integers?
Interaction between SFINAE and Overloaded Functions
How does SFINAE interact with normal overloaded functions?
SFINAE vs Concepts: Which should I use?
I've heard that C++20 Concepts can replace SFINAE. Should I still learn SFINAE?
Using SFINAE to Check for Member Types
Can I use SFINAE to check if a type has a specific member type?
Interaction between SFINAE and Default Template Arguments
How does SFINAE work with default template arguments?
Applying SFINAE to Variadic Templates
Can SFINAE be used with variadic templates?
Tag Dispatching vs SFINAE
What is tag dispatching and how does it differ from SFINAE?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant