enable-if

What is the recommended way to simulate concepts and constraints? [closed]

感情迁移 提交于 2019-12-07 06:35:17
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 12 months ago . Before the introduction of concepts and constraints, there are several ways to simulate this compile-time check. Take a " order() " function for example: (how to implement LessThanComparable without concepts or constraints is another story) Use static_assert template

why SFINAE (enable_if) works from inside class definition but not from outside

非 Y 不嫁゛ 提交于 2019-12-07 06:27:09
问题 Very weird problem I've been struggling with for the past few hours (after solving 5-6 other issues with SFINAE as I'm new to it). Basically in the following code I want to have f() working for all possible template instantiations, but have g() available only when N == 2 : #include <type_traits> #include <iostream> template<typename T, int N> class A { public: void f(void); void g(void); }; template<typename T, int N> inline void A<T, N>::f() { std::cout << "f()\n"; } template<typename T, int

Enable template only for specific templated class

北城余情 提交于 2019-12-07 02:05:56
问题 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;

Where is disable_if in C++0x?

白昼怎懂夜的黑 提交于 2019-12-06 16:58:47
问题 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. 回答1: 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

What are the syntax and semantics of C++ templated code?

▼魔方 西西 提交于 2019-12-06 06:51:34
template<typename T, size_t M, size_t K, size_t N, typename std::enable_if_t<std::is_floating_point<T>::value, T> = 0> void fastor2d(){//...} I copied this line of code from cpp-reference(only the std::enable_if part, i do need T and all three of the size_t 's), because i would like to use this function only when floating_types are used on it ... it does not compile. Could somebody explain to me, why, and what it even does? While i am at it, how do you call this function afterwards? Every tutorial or question here on SO gets bombed with answers, and that is great, but to someone who does not

enable_if + disable_if combination provokes an ambiguous call

半城伤御伤魂 提交于 2019-12-06 03:20:57
问题 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

Can I overload template variables?

旧街凉风 提交于 2019-12-06 01:39:02
问题 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>

C++ boost enable_if question

霸气de小男生 提交于 2019-12-06 00:55:35
Do I have any way to simplify the following statements? (probably, using boost::enable_if ) . I have a simple class structure - Base base class, Derived1 , Derived2 inherit from Base . I have the following code: template <typename Y> struct translator_between<Base, Y> { typedef some_translator<Base, Y> type; }; template <typename Y> struct translator_between<Derived1, Y> { typedef some_translator<Derived1, Y> type; }; template <typename Y> struct translator_between<Derived2, Y> { typedef some_translator<Derived2, Y> type; }; I want to write the same statement using one template specialization

why SFINAE (enable_if) works from inside class definition but not from outside

爷,独闯天下 提交于 2019-12-05 11:36:00
Very weird problem I've been struggling with for the past few hours (after solving 5-6 other issues with SFINAE as I'm new to it). Basically in the following code I want to have f() working for all possible template instantiations, but have g() available only when N == 2 : #include <type_traits> #include <iostream> template<typename T, int N> class A { public: void f(void); void g(void); }; template<typename T, int N> inline void A<T, N>::f() { std::cout << "f()\n"; } template<typename T, int N, typename std::enable_if<N == 2, void>::type* = nullptr> inline void A<T, N>::g() { std::cout << "g(

enable_if type is not of a certain template class

为君一笑 提交于 2019-12-05 11:04:35
TLDR: See the last paragraph. I have an operator& defined for several template classes like so: template <typename T> struct Class { Class(T const &t) { } }; template <typename T_Lhs, typename T_Rhs> struct ClassAnd { ClassAnd(T_Lhs const &lhs, T_Rhs const &rhs) { } }; template <typename T, typename T_Rhs> ClassAnd<Class<T>, T_Rhs> operator&(Class<T> const &lhs, T_Rhs const &rhs) { return ClassAnd<Class<T>, T_Rhs>(lhs, rhs); } template <typename T0, typename T1, typename T_Rhs> ClassAnd<ClassAnd<T0, T1>, T_Rhs> operator&(ClassAnd<T0, T1> const &lhs, T_Rhs const &rhs) { return ClassAnd<ClassAnd