SUNphi  1.0
SFINAE.hpp File Reference

Defines macros to enable or disable template instantiation. More...

Go to the source code of this file.

Macros

#define SFINAE_ON_TEMPLATE_ARG(...)   TypeIf<(__VA_ARGS__),void*> =nullptr
 
#define SFINAE_WORSEN_DEFAULT_VERSION_TEMPLATE_PARS   typename...DummyTypes /* Fake list of types */
 
#define SFINAE_WORSEN_DEFAULT_VERSION_ARGS   DummyTypes... /*< Fake list of args */
 Provide empty list of args, used to unprioritize default version.
 
#define SFINAE_WORSEN_DEFAULT_VERSION_ARGS_CHECK   STATIC_ASSERT_ARE_N_TYPES(0,DummyTypes)
 Check that no extra arg is passed.
 
#define SFINAE_TEMPLATE_CLASS_SPECIALIZATION_PREAMBLE
 
#define SFINAE_TEMPLATE_CLASS_SPECIALIZATION_ARG(TYPE)   EnableIfIs ## TYPE<TT<Ts...>,TT<Ts...>>
 To be used as an argument of the specialization.
 
#define SFINAE_TEMPLATE_CLASS_SPECIALIZATION_PROVIDE_TYPE
 Provides the type inside the specialized class. More...
 

Detailed Description

Defines macros to enable or disable template instantiation.

http://en.cppreference.com/w/cpp/language/sfinae

Definition in file SFINAE.hpp.

Macro Definition Documentation

#define SFINAE_ON_TEMPLATE_ARG (   ...)    TypeIf<(__VA_ARGS__),void*> =nullptr

Provides a SFINAE to be used in template par list

This follows https://stackoverflow.com/questions/32636275/sfinae-with-variadic-templates as in this example

1 template <typename D,
2  SFINAE_ON_TEMPLATE_ARG(IsSame<D,int>)>
3 void foo(D i) {} // fails if D is not int

Definition at line 24 of file SFINAE.hpp.

#define SFINAE_TEMPLATE_CLASS_SPECIALIZATION_PREAMBLE
Value:
template <template<typename...> typename TT, \
typename...Ts>

SFINAE for template class specialisation

Mechanism to allow the usage of SFINAE to allow a class specialization. Follows the advice of https://stackoverflow.com/a/30991097 to create a class providing the type itself

Example:

1 Class which needs to be explicitly specialized with SFINAE
2 template <typename T>
3 class SpecializableClass
4 {
5 };
6 
7 /// Dummy type to be used to specialize the class
8 class SpecializingArg
9 {
10 };
11 
12 PROVIDE_ENABLE_IF_FOR_TYPE(SpecializingArg);
13 
14 /// Specializes the class SpecializableClass
15 SFINAE_TEMPLATE_CLASS_SPECIALIZATION_PREAMBLE
16 class SpecializableClass<SFINAE_TEMPLATE_CLASS_SPECIALIZATION_ARG(SpecializingArg)>
17  {
18  SFINAE_TEMPLATE_CLASS_SPECIALIZATION_PROVIDE_TYPE;
19 
20  /// To show explicit usage of T
21  SpecializableClass<T>()
22  {
23  }
24  };

Definition at line 108 of file SFINAE.hpp.

#define SFINAE_TEMPLATE_CLASS_SPECIALIZATION_PROVIDE_TYPE
Value:
\
using T= \
TT<Ts...>
Tens< Tk, double > T
Tensor class of the expression.
Definition: RelBind.hpp:263

Provides the type inside the specialized class.

Definition at line 117 of file SFINAE.hpp.

#define SFINAE_WORSEN_DEFAULT_VERSION_TEMPLATE_PARS   typename...DummyTypes /* Fake list of types */

Provides template par list to unprioritize default SFINAE

Use as last argument of a function overloaded by a other implementations using SFINAE to detect the proper version to be used. This has to be used in conjunction with the other macros SFINAE_WORSEN_DEFAULT_VERSION_ARGS and SFINAE_WORSEN_DEFAULT_VERSION_ARGS_CHECK as in this example

1 template <typename T,
2  SFINAE_WORSEN_DEFAULT_VERSION_TEMPLATE_PARS>
3 int tell(T a,SFINAE_WORSEN_DEFAULT_VERSION_ARGS)
4 {
5  SFINAE_WORSEN_DEFAULT_VERSION_ARGS_CHECK;
6  return 1;
7 }
8 
9 template <typename T,
10  std::enable_if_t<sizeof(T)==4,void*> =nullptr>
11 decltype(auto) tell(T&& a)
12 {
13  return a+1;
14 }
15 
16 int main()
17 {
18  tell(1); //returns 2
19 
20  return 0;
21 }

Definition at line 58 of file SFINAE.hpp.