This is my simplified code :
template<typename device, template<typename device> class protocol>
class MyClass
{
public:
template<typename select>
bool method()
{
// Code
}
};
I want method
to act different in terms of the protocol
type.
In other words, I have two different possible protocols, and I want to have two different behaviours for my method according to the protocols. But I don't know how to write it with templates.
By example, using SFINAE (if you accept a C++11 solution)
--- EDIT ---
As pointed by Yakk (thanks!), this solution is weak because use a template default value that can be explicited and circunvented.
An example; with
is called the "protocol_1" version of
method()
, using the default value forp
; but explcitingp
, as followsis called the "protocol_2" version of
method()
over an istance of aMyClass
based onprotocol_1
To avoid this problem it's possible add a
static_assert()
, in both version ofmethod()
, to check and impose thatp
is equal to its default value (protocol<device>
)I mean... as follow
So
generate a compiler error.