, MyClass ,
:
double (*)(const T &var);
bool (*)(const T &var);
T , , -
:
double (C::*)(const T &var);
bool (C::*)(const T &var);
C T, MyClass
T C :
(1) non-class C -,
.
(2) C , -,
--.
C void. C
void:
template<typename T, typename C = void>
struct MyClass;
, :
MyClass<T>
T :
MyClass<T,C>
C, void, -.
, std::enable_if
SFINAE,
, ,
U .
, , :
, :
template<typename T>
struct MyClass<T>
{
... for free function pointers ...
};
template<typename T, typename C>
struct MyClass<T,C>
{
... for member function pointers ...
};
, - ""
, . ,
, .
, ,
, ,
.
template <typename T, typename C = void, typename Default = void>
struct MyClass;
, :
template <typename T, typename C = void, typename Default = void>
struct MyClass;
template <typename T>
struct MyClass<T>
{
using firstFunctor_t = double(*)(T const &);
using secondFunctor_t = bool(*)(T const &);
MyClass(firstFunctor_t firstFunc, secondFunctor_t secondFunc)
: _firstFunc(firstFunc),
_secondFunc(secondFunc)
{}
double callFirst(T const & var) {
return _firstFunc(var);
}
bool callSecond(T const & var) {
return _secondFunc(var);
}
private:
firstFunctor_t _firstFunc;
secondFunctor_t _secondFunc;
};
template <typename T, typename C>
struct MyClass<T,C>
{
using firstFunctor_t = double(C::*)(T const &);
using secondFunctor_t = bool(C::*)(T const &) const;
MyClass(firstFunctor_t firstFunc, secondFunctor_t secondFunc)
: _firstFunc(firstFunc),
_secondFunc(secondFunc)
{}
double callFirst(C & obj, T const & var) {
return (obj.*_firstFunc)(var);
}
double callFirst(C const & obj, T const & var) {
auto & o = const_cast<C&>(obj);
return (o.*_firstFunc)(var);
}
bool callSecond(C & obj, T const & var) {
return (obj.*_secondFunc)(var);
}
bool callSecond(C const & obj, T const & var) {
auto & o = const_cast<C&>(obj);
return (o.*_secondFunc)(var);
}
private:
firstFunctor_t _firstFunc;
secondFunctor_t _secondFunc;
};
- ,
: -
, -, ,
const. , - C
T const & bool const
, ? , const-ness
-, :
using secondFunctor_t = bool(C::*)(T const &) const;
bool (C::*)(T const &) const
.
, MyClass<T,C>::callFirst
MyClass<T,C>::callSecond, :
C & obj, T const & var
:
C const & obj, T const & var
MyClass<T,C>::callFirst
MyClass<T,C>::callSecond obj, ,
.
:
#include <iostream>
#include <string>
double foo(std::string const & s)
{
return std::stod(s);
}
bool bar(std::string const & s)
{
return s.size() > 0;
}
struct SomeClass
{
SomeClass(){};
double foo(std::string const & s) {
return ::foo(s);
}
bool bar(std::string const & s) const {
return ::bar(s);
}
};
int main()
{
MyClass<std::string> my0{foo,bar};
std::cout << std::boolalpha;
std::cout << my0.callFirst("1.11") << std::endl;
std::cout << my0.callSecond("Hello World") << std::endl;
MyClass<std::string,SomeClass> my1{&SomeClass::foo,&SomeClass::bar};
SomeClass thing;
std::cout << my1.callFirst(thing,"2.22") << std::endl;
std::cout << my1.callSecond(thing,"Hello World") << std::endl;
SomeClass const constThing;
std::cout << my1.callFirst(constThing,"3.33") << std::endl;
std::cout << my1.callSecond(constThing,"Hello World") << std::endl;
return 0;
}
, , " ".
,
, , , .
-, , [member]
.
. .