What is an implicit interface in templates?

Share the Article

Implicit interface is concept which is based on C++ templates. This concept works in conjunction with compile-time polymorphism.

Explicit Interface

In runtime polymorphism, the concept of interfaces is discussed in context of explicit interfaces. In such interfaces, the functions and behavior is explicitly visible, like,

  • Details regarding the member functions of class
  • Details of the member function signatures (parameters & return types)
  • Which all functions shall be private and which all public
class MFBase { public: virtual void funda1(int ix, float fy) = 0; virtual int funda2(std::string& str) = 0; };

The interface of class MFBase, explicitly defines 2 pure virtual functions. The return type and argument type specifies the explicit interface.

Implicit Interfaces

In world of templates, the concept of interfaces is discussed in implicit interface terms, like, what all valid expressions the interface need support. Actually, it is the use-model of expressions which define requirement on the interface (templates).

template<typename T> void doWork(T& w) { if (w.funda2("main") > 10 && w != nullptr) { } }

Explaination:

The implicit interface specified by type T should have following requirements:

  • Firstly, it must have a member function named funda2( ) which takes string and returns an integer type value.
  • Secondly, it must have a valid operator != function which can compare two objects of type T and return Boolean value.

Calling function with incompatible implicit template

In following example, the main function calls dowork<T> with MainFunda object. However, this class’s interface is not compatible as it does not define a not-equal-to operator (!=).

Therefore, the following program shall throw a compiler error

#include<iostream> //main header using namespace std;//namespace class MainFunda { public: int funda2(std::string str) { return str.size(); } }; template<typename T> void dowork(T& w) { if (w.funda2("main funda class") > 10 && w != nullptr) { cout << "Success" << endl; } } int main() { MainFunda m1; dowork(m1); return 0; }

Compiler error

calling function with incompatible implicit template

How to resolve the error

The error is fixed by providing not-equal-to (!=) function in MainFunda class. This shall satisfy the implicit interface requirement of template parameter in dowork. For any other template function the implicit interface requirement shall be different.

Now, in below code, new function is available (in bold)

#include<iostream> //main header using namespace std;//namespace class MainFunda { public: int funda2(std::string str) { return str.size(); } bool operator!=(nullptr_t np) { return 1; } }; template<typename T> void dowork(T& w) { if (w.funda2("main funda class") > 10 && w != nullptr) { cout << "Success" << endl; } } int main() { MainFunda m1; dowork(m1); return 0; }

Please note, it is not necessary to only have a user-defined operator !=, the requirement can be satisfied by other ways.

One Point

Here, the T can be converted to another type X & nullptr can be converted to some type Y. Therefore, if X and Y are comparable and there is implicit conversion between X and Y types, the requirement will be satisfied.

Main Funda: What all valid expressions the interface need to support specifies implicit interface

Related Topics:

What are dependent scope type in templates?
What is an implicit interface? 
Calling member functions in template base class
What is template meta programming (TMP)
How delete keyword can be used to filter polymorphism
Concept of Inline Functions
What is reference collapsing?
How std::forward( ) works?
How std::move() function works?
Smart Pointers: shared_ptr <T> 
Smart Pointers: unique_ptr<T>
What is move constructor ?
Understanding Constant Variables

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *