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
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.