mlpack
Classes | Namespaces | Macros
sfinae_utility.hpp File Reference
#include <type_traits>
#include <cstring>
Include dependency graph for sfinae_utility.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, AdditionalArgsCount >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 0 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 1 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 2 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 3 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 4 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 5 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 6 >
 
struct  mlpack::sfinae::MethodFormDetector< Class, MethodForm, 7 >
 
struct  mlpack::sfinae::SigCheck< U, U >
 Utility struct for checking signatures. More...
 

Namespaces

 mlpack
 Linear algebra utility functions, generally performed on matrices or vectors.
 

Macros

#define HAS_MEM_FUNC(FUNC, NAME)
 
#define HAS_METHOD_FORM_BASE(METHOD, NAME, MAXN)
 Base macro for HAS_METHOD_FORM() and HAS_EXACT_METHOD_FORM() macros.
 
#define HAS_ANY_METHOD_FORM(FUNC, NAME)
 Constructs a template structure, which will define a boolean static variable, to true, if the passed template parameter, has a member function with the specified name. More...
 
#define SINGLE_ARG(...)   __VA_ARGS__
 
#define HAS_METHOD_FORM(METHOD, NAME)   HAS_METHOD_FORM_BASE(SINGLE_ARG(METHOD), SINGLE_ARG(NAME), 7)
 HAS_METHOD_FORM generates a template that allows a compile time check for whether a given class has a method of the requested form. More...
 
#define HAS_EXACT_METHOD_FORM(METHOD, NAME)   HAS_METHOD_FORM_BASE(SINGLE_ARG(METHOD), SINGLE_ARG(NAME), 0)
 HAS_EXACT_METHOD_FORM generates a template that allows a compile time check whether a given class has a method of the requested form. More...
 

Detailed Description

Author
Trironk Kiatkungwanglai, Kirill Mishchenko

This file contains macro utilities for the SFINAE Paradigm. These utilities determine if classes passed in as template parameters contain members at compile time, which is useful for changing functionality depending on what operations an object is capable of performing.

mlpack is free software; you may redistribute it and/or modify it under the terms of the 3-clause BSD license. You should have received a copy of the 3-clause BSD license along with mlpack. If not, see http://www.opensource.org/licenses/BSD-3-Clause for more information.

Macro Definition Documentation

◆ HAS_ANY_METHOD_FORM

#define HAS_ANY_METHOD_FORM (   FUNC,
  NAME 
)
Value:
template <typename T> \
struct NAME \
{ \
template <typename Q = T> \
static typename \
std::enable_if<std::is_member_function_pointer<decltype(&Q::FUNC)>::value, \
int>::type \
f(int) { return 1;} \
\
template <typename Q = T> \
static char f(char) { return 0; } \
\
static const bool value = sizeof(f<T>(0)) != sizeof(char); \
};

Constructs a template structure, which will define a boolean static variable, to true, if the passed template parameter, has a member function with the specified name.

The check does not care about the signature or the function parameters.

Parameters
FUNCthe name of the function, whose existence is to be detected
NAMEthe name of the structure that will be generated

Use this like: NAME<ClassName>::value to check for the existence of the function in the given class name. This can also be used in conjunction with std::enable_if.

◆ HAS_EXACT_METHOD_FORM

#define HAS_EXACT_METHOD_FORM (   METHOD,
  NAME 
)    HAS_METHOD_FORM_BASE(SINGLE_ARG(METHOD), SINGLE_ARG(NAME), 0)

HAS_EXACT_METHOD_FORM generates a template that allows a compile time check whether a given class has a method of the requested form.

For example, for the following class

class A { public: ... Train(const arma::mat&, const arma::Row<size_t>&); ... };

and the following form of Train methods

template<typename Class> using TrainForm = void(Class::*)(const arma::mat&, const arma::Row<size_t>&);

we can check whether the class A has a Train method of the specified form:

HAS_METHOD_FORM(Train, HasTrain); static_assert(HasTrain<A, TrainFrom>::value, "value should be true");

The class generated by this will only return true values if the signature matches exactly.

Parameters
METHODThe name of the method to check for.
NAMEThe name of the struct to construct.

◆ HAS_MEM_FUNC

#define HAS_MEM_FUNC (   FUNC,
  NAME 
)
Value:
template<typename T, typename sig, typename = std::true_type> \
struct NAME : std::false_type {}; \
\
template<typename T, typename sig> \
struct NAME \
< \
T, \
sig, \
std::integral_constant<bool, mlpack::sfinae::SigCheck<sig, &T::FUNC>::value> \
> : std::true_type {};

◆ HAS_METHOD_FORM

#define HAS_METHOD_FORM (   METHOD,
  NAME 
)    HAS_METHOD_FORM_BASE(SINGLE_ARG(METHOD), SINGLE_ARG(NAME), 7)

HAS_METHOD_FORM generates a template that allows a compile time check for whether a given class has a method of the requested form.

For example, for the following class

class A { public: ... Train(const arma::mat&, const arma::Row<size_t>&, double); ... };

and the following form of Train methods

template<typename Class, typename...Ts> using TrainForm = void(Class::*)(const arma::mat&, const arma::Row<size_t>&, Ts...);

we can check whether the class A has a Train method of the specified form:

HAS_METHOD_FORM(Train, HasTrain); static_assert(HasTrain<A, TrainFrom>::value, "value should be true");

The class generated by this will also return true values if the given class has a method that also has extra parameters.

Parameters
METHODThe name of the method to check for.
NAMEThe name of the struct to construct.