Function Binding and Partial Application

Binding and Function Templates

Can std::bind be used with function templates?

Abstract art representing computer programming

Yes, std::bind() can be used with function templates. When binding a function template, you need to provide the template arguments explicitly or let the compiler deduce them based on the bound arguments.

Consider the following example:

#include <functional>
#include <iostream>
#include <string>

template <typename T>
void PrintValue(T Value) {
  std::cout << Value << '\n';
}

int main() {
  auto PrintInt{std::bind(
    PrintValue<int>, std::placeholders::_1)};

  auto PrintString{std::bind(
    PrintValue<std::string>,
    std::placeholders::_1)};

  PrintInt(42);
  PrintString("Hello");
}
42
Hello

In this example, we have a function template PrintValue that takes a single argument of any type T and prints its value. When binding PrintValue using std::bind, we explicitly provide the template argument (int or std::string) to specialize the function template. The resulting PrintInt and PrintString functors are bound to the specialized versions of PrintValue for int and std::string respectively.

However, using std::bind to deduce the template arguments directly is not straightforward and often not possible. Instead, you can use a lambda function to achieve the desired behavior with template argument deduction:

#include <iostream>
#include <string>

template <typename T>
void PrintValue(T Value) {
  std::cout << Value << '\n';
}

int main() {
  auto PrintValueAuto{
    [](auto Value) { PrintValue(Value); }
  };

  PrintValueAuto(42);
  PrintValueAuto("Hello");
}
42
Hello

In this case, we use a lambda function that takes an auto parameter. The compiler deduces the type of the parameter when the lambda is called, allowing PrintValue to be called with the correct template argument. This approach provides the flexibility of template argument deduction without the need for std::bind in this context.

Binding function templates allows you to create reusable functors that can work with different types, providing flexibility and generic programming capabilities.

This Question is from the Lesson:

Function Binding and Partial Application

This lesson covers function binding and partial application using std::bind(), std::bind_front(), std::bind_back() and std::placeholders.

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

This Question is from the Lesson:

Function Binding and Partial Application

This lesson covers function binding and partial application using std::bind(), std::bind_front(), std::bind_back() and std::placeholders.

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