Type Traits: Compile-Time Type Analysis

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?

Illustration representing computer hardware

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved