Using Type Traits with Class Templates

Can I use type traits to conditionally enable or disable certain member functions in a class template based on the template type?

Yes, you can use type traits in combination with SFINAE (Substitution Failure Is Not An Error) to conditionally enable or disable member functions in a class template based on the properties of the template type. Here's an example:

#include <iostream>
#include <type_traits>

template <typename T>
class MyClass {
public:
  // Enable this function only if T
  // is an arithmetic type
  template <typename U = T,
    typename = std::enable_if_t<
      std::is_arithmetic_v<U>>>
  void doArithmeticOperation(U value) {
    std::cout << "Doing arithmetic operation"
      " with " << value << "\n";
  }

  // Enable this function only if T
  // is a class type
  template <typename U = T,
    typename = std::enable_if_t<
      std::is_class_v<U>>>
  void doClassOperation(const U& obj) {
    std::cout << "Doing class operation\n";
  }
};

class SomeClass {};

int main() {
  MyClass<int> intClass;
  intClass.doArithmeticOperation(42);

  // This would be a compiler error
  // intClass.doClassOperation(SomeClass{});

  MyClass<SomeClass> classClass;
  classClass.doClassOperation(SomeClass{});

  // This would be a compiler error
  // classClass.doArithmeticOperation(42);
}
Doing arithmetic operation with 42
Doing class operation

In this example, the MyClass class template has two member functions: doArithmeticOperation and doClassOperation. These functions are conditionally enabled based on the properties of the template type T.

The doArithmeticOperation function is enabled only if T is an arithmetic type, using std::enable_if_t and std::is_arithmetic_v. Similarly, the doClassOperation function is enabled only if T is a class type, using std::enable_if_t and std::is_class_v.

When instantiating MyClass with int, only the doArithmeticOperation function is available. When instantiating MyClass with SomeClass, only the doClassOperation function is available. Attempting to call a disabled member function will result in a compiler error.

This technique allows you to create class templates that adapt their interface and behavior based on the properties of the template type, providing a more flexible and type-safe design.

Type Traits: Compile-Time Type Analysis

Learn how to use type traits to perform compile-time type analysis, enable conditional compilation, and enforce type requirements in templates.

Questions & Answers

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

Using std::is_base_of with Template Types
How can I use std::is_base_of to check if a template type is derived from a specific base class?
Creating a Custom Type Trait to Check for a Member Function
How can I create a custom type trait to check if a type has a specific member function?
Using Type Traits with Function Templates
How can I use type traits to provide different implementations of a function template based on the properties of the template type?
Using Type Traits with Template Specialization
Can I use type traits to conditionally specialize a class template based on the properties of the template type?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant