enable-if

Enable template only for specific templated class

自作多情 提交于 2019-12-05 06:38:28
This question is inspired from my previous question No template parameter deduction of parameter pack . Consider following code example: #include <memory> #include <string> template<typename... FArgs> class Callback { public: class Handle{}; }; class BaseCallbackHandle { }; using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>; template<typename H> TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle) { return {}; } int main() { Callback<int>::Handle h; std::string s; makeTypeErasedCallbackHandle(h); //should compile fine makeTypeErasedCallbackHandle(s); //should

SFINAE enable_if explicit constructor

假装没事ソ 提交于 2019-12-05 02:41:50
I'm trying to switch between an explicit and an implicit conversion constructor via enable_if . My code currently looks like #include <type_traits> #include <cstdint> enum class enabled {}; template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type; template <bool B, typename T = void> using disable_if_t = typename std::enable_if<!B, T>::type; template <std::intmax_t A> struct SStruct { static constexpr std::intmax_t a = A; }; template <typename T> struct SCheckEnable : std::integral_constant<bool, T::a == 0> { }; template <typename U, typename T> class CClass

Optionally supporting initializer_list construction for templates maybe wrapping containers

对着背影说爱祢 提交于 2019-12-05 01:38:13
If I have a template that wraps a standard container, it seems I can reasonably easily delegate the initializer_list constructor: template<typename T> struct holder { T t_; holder() : t_() {} holder(std::initializer_list<typename T::value_type> values) : t_(values) {} }; So this works nicely with std::vector, for instance. int main(int argc, char* argv[]) { holder<std::vector<int>> y{1,2,3}; return EXIT_SUCCESS; } But it pretty obviously doesn't work for T as 'int', or any other type that doesn't have a nested value_type typedef. So, I'd like to use some sort of enable_if or similar trick to

Where is disable_if in C++0x?

半城伤御伤魂 提交于 2019-12-04 22:33:05
Boost has both enable_if and disable_if , but C++0x seems to be missing the latter. Why was it left out? Are there meta-programming facilities in C++0x that allow me to build disable_if in terms of enable_if ? Oh, I just noticed that std::enable_if is basically boost::enable_if_c , and that there is no such thing as boost::enable_if in C++0x. At the risk of seeming stupid, just do !expression instead of expression in the bool template parameter in enable_if to make it behave like a disable_if ? Of course if that idea works, you could just expand on it to write a class with disable_if -like

How do I use std::enable_if with a self-deducing return type?

≡放荡痞女 提交于 2019-12-04 08:28:30
问题 C++14 will have functions whose return type can be deduced based on the return value. auto function(){ return "hello world"; } Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom? For example, let's consider the following two functons: #include <type_traits> #include <iostream> //This function is chosen when an integral type is passed in template<class T > auto function(T t) -> typename std::enable_if<std::is_integral<T>::value>::type { std::cout <<

enable_if + disable_if combination provokes an ambiguous call

丶灬走出姿态 提交于 2019-12-04 07:27:44
While trying to answer this question I wanted to suggest the use of enable_if + disable_if to allow the overload of a method based on the fact that a type was (or not) polymorphic. So I created a small test file: template <class T> void* address_of(T* p, boost::enable_if< boost::is_polymorphic<T> >* dummy = 0) { return dynamic_cast<void*>(p); } template <class T> void* address_of(T* p, boost::disable_if< boost::is_polymorphic<T> >* dummy = 0) { return static_cast<void*>(p); } struct N { int x; }; int main(int argc, char* argv[]) { N n; std::cout << address_of(&n) << std::endl; return 0; }

Can I overload template variables?

ⅰ亾dé卋堺 提交于 2019-12-04 06:02:00
I want to declare something like this: template <typename T> constexpr enable_if_t<is_integral_v<T>, int[]> foo = { 1, 2 }; template <typename T> constexpr enable_if_t<is_floating_point_v<T>, int[]> foo = { 10, 20, 30 }; But when I try to I'm getting this error : error: redeclaration of template<class T> constexpr std::enable_if_t<std::is_floating_point<_Tp>::value, int []> foo note: previous declaration template<class T> constexpr std::enable_if_t<std::is_integral<_Tp>::value, int []> foo<T> I feel like this should be legal as there will never be more than one foo defined for any given

Understanding Alias Templates

£可爱£侵袭症+ 提交于 2019-12-04 03:51:31
问题 I asked a question that has several references to the code: template <typename...> using void_t = void; I believe I have a generally misunderstand alias templates: Why wouldn't you just evaluate whatever template parameter you're passing into an alias template in an enable_if_t or conditional_t statement? Is the code above just about doing an enable_if_t on multiple template parameters at once? Secondly, I believe that I have a specific misunderstanding of the role of void_t . This comment

C++ - Iterating over a tuple & resolution of type vs constant parameters

时光毁灭记忆、已成空白 提交于 2019-12-03 17:09:40
问题 I'm currently in the process of writing arithmetic operator overloads for tuples. The operator iterates over the tuple to perform the operation on each of its individual element. Here is the definition for operator +=: template< typename... Ts, std::size_t I = 0 > inline typename std::enable_if< I == sizeof... (Ts), std::tuple< Ts... >& >::type operator +=(std::tuple< Ts... >& lhs, const std::tuple< Ts... >& rhs) { return lhs; } template< typename... Ts, std::size_t I = 0 > inline typename

std::enable_if or SFINAE for iterator or pointer

▼魔方 西西 提交于 2019-12-03 14:35:54
I would like to write a constructor for MyClass that take an argument and I want this to compile only if the argument is a pointer or an iterator (something having iterator_traits ). How to achieve this ? Regrettably, there is no standard way to detect whether a class models Iterator . The simplest check would be that *it and ++it are both syntactically valid; you can do this using standard SFINAE techniques: template<typename T, typename = decltype(*std::declval<T&>(), void(), ++std::declval<T&>(), void())> MyClass(T); Considering the Iterator requirements from 24.2.2:2: template<typename T>