sfinae_utility.hpp File Reference
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  MethodFormDetector< Class, MethodForm, AdditionalArgsCount >
 
struct  MethodFormDetector< Class, MethodForm, 0 >
 
struct  MethodFormDetector< Class, MethodForm, 1 >
 
struct  MethodFormDetector< Class, MethodForm, 2 >
 
struct  MethodFormDetector< Class, MethodForm, 3 >
 
struct  MethodFormDetector< Class, MethodForm, 4 >
 
struct  MethodFormDetector< Class, MethodForm, 5 >
 
struct  MethodFormDetector< Class, MethodForm, 6 >
 
struct  MethodFormDetector< Class, MethodForm, 7 >
 
struct  SigCheck< U, U >
 Utility struct for checking signatures. More...

 

Namespaces

 mlpack
 
.hpp
 
 mlpack::sfinae
 

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

 
#define HAS_MEM_FUNC(FUNC, NAME)
 
#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_METHOD_FORM_BASE(METHOD, NAME, MAXN)
 Base macro for HAS_METHOD_FORM() and HAS_EXACT_METHOD_FORM() macros. More...

 
#define SINGLE_ARG(...)   __VA_ARGS__
 

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.

Definition in file sfinae_utility.hpp.

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.

Definition at line 201 of file sfinae_utility.hpp.

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

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.
MAXNThe maximum number of additional arguments.

Definition at line 287 of file sfinae_utility.hpp.

◆ 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 {};

Definition at line 128 of file sfinae_utility.hpp.

◆ 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.
MAXNThe maximum number of additional arguments.

Definition at line 253 of file sfinae_utility.hpp.

◆ HAS_METHOD_FORM_BASE

#define HAS_METHOD_FORM_BASE (   METHOD,
  NAME,
  MAXN 
)

Base macro for HAS_METHOD_FORM() and HAS_EXACT_METHOD_FORM() macros.

Definition at line 143 of file sfinae_utility.hpp.

◆ SINGLE_ARG

#define SINGLE_ARG (   ...)    __VA_ARGS__

Definition at line 220 of file sfinae_utility.hpp.