enable-if

enable_if and conversion operator?

江枫思渺然 提交于 2019-11-29 08:43:34
问题 Any chance to use enable_if with a type conversion operator? Seems tricky, since both return type and parameters list are implicit. 回答1: dixit the documentation: There does not seem to be a way to specify an enabler for a conversion operator. Converting constructors, however, can have enablers as extra default arguments. 回答2: From the little research I did (and ignoring the c++0x comment from Johannes), my answer is that it depends what you want the enable_if for. If you want the conversion

Is it possible to use 'enable_if' and 'is_same' with variadic function templates?

淺唱寂寞╮ 提交于 2019-11-29 07:13:34
These two non-variadic function templates do compile: template <typename T, typename U> typename std::enable_if<std::is_same<U, int>::value, void>:: type testFunction(T a, U b) { std::cout << "b is integer\n"; } template <typename T, typename U> typename std::enable_if<std::is_same<U, float>::value, void>:: type testFunction(T a, U b) { std::cout << "b is float\n"; } however, similar variadic templates do not compile: template <typename T, typename... U> typename std::enable_if<std::is_same<U, int>::value, void>:: type testFunction(T a, U... bs) { std::cout << "bs are integers\n"; } template

C++ templates: conditionally enabled member function

北城余情 提交于 2019-11-29 02:08:47
I'm creating a very small C++ project, and I'd like to create a simple vector class for my own needs. The std::vector template class will not do. When the vector class is comprised of char s (i.e. vector<char> ), I'd like it to be able to be compared to a std::string . After a bit of messing around, I wrote code that both compiles and does what I want. See below: #include <string> #include <stdlib.h> #include <string.h> template <typename ElementType> class WorkingSimpleVector { public: const ElementType * elements_; size_t count_; // ... template <typename ET = ElementType> inline typename

Add/Remove data members with template parameters?

不想你离开。 提交于 2019-11-28 23:58:46
问题 Consider the following code : template<bool AddMembers> class MyClass { public: void myFunction(); template<class = typename std::enable_if<AddMembers>::type> void addedFunction(); protected: double myVariable; /* SOMETHING */ addedVariable; }; In this code, the template parameter AddMembers allow to add a function to the class when it's true . To do that, we use an std::enable_if . My question is : is the same possible (maybe with a trick) for data members variable ? (in a such way that

how can I use std::enable_if in a conversion operator?

我与影子孤独终老i 提交于 2019-11-28 12:18:49
Basically I want my range type to be implicitly convertible from Range<const char> to Range<const unsigned char> . std::enable_if seems impossible because the function takes no arguments and has no return. Whats the work around? Here is basically what I tried: template<typename T> class Range{ T* begin_; T* end_; public: Range(T* begin,T* end):begin_{begin},end_{end}{} template<int N> Range(T (&a)[N]):begin_{static_cast<T*>(&a[0])},end_{static_cast<T*>(&a[N-1])}{} T* Begin(){return begin_;} T* End(){return end_;} operator typename std::enable_if<std::is_same<T,const char>::value,Range<const

enable_if in template Parameters Creates Template Redefinition Error

只谈情不闲聊 提交于 2019-11-28 06:27:19
问题 In this answer what I really wanted to do is define a typename in my template parameters which could be used in the cast and return. So this: template <typename T> typename std::enable_if<sizeof(unsigned char) == sizeof(T), unsigned char>::type caster(T value){ return reinterpret_cast<unsigned char&>(value); } Would become this: template <typename T, typename R = std::enable_if<sizeof(unsigned char) == sizeof(T), unsigned char>::type > R caster(T value){ return reinterpret_cast<R&>(value); }

Why does enable_if_t in template arguments complains about redefinitions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 21:06:30
I have the following case that works using std::enable_if : template<typename T, typename std::enable_if<std::is_same<int, T>::value>::type* = nullptr> void f() { } template<typename T, typename std::enable_if<std::is_same<double, T>::value>::type* = nullptr> void f() { } Now, I saw in cppreference the new syntax, much cleaner in my opinion : typename = std::enable_if_t<std::is_same<int, T>::value>> I wanted to port my code : template<typename T, typename = std::enable_if_t<std::is_same<int, T>::value>> void g() { } template<typename T, typename = std::enable_if_t<std::is_same<double, T>:

Enable method based on boolean template parameter

[亡魂溺海] 提交于 2019-11-27 18:35:07
问题 I want to implement a private function based on a boolean template parameter. Something like that: #include <iostream> using namespace std; template <bool is_enabled = true> class Aggregator { public: void fun(int a) { funInternal(a); } private: void funInternal(int a, typename std::enable_if<is_enabled>::type* = 0) { std::cout << "Feature is enabled!" << std::endl; } void funInternal(int a, typename std::enable_if<!is_enabled>::type* = 0) { std::cout << "Feature is disabled!" << std::endl; }

Default template argument when using std::enable_if as templ. param.: why OK with two template functions that differ only in the enable_if parameter?

跟風遠走 提交于 2019-11-27 14:46:05
In the language reference of std::enable_if at cppreference the following note is included Notes A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal. In the template functions in example below, it seems to me that this situation occurs. I.e., the two template functions onlyForDerivedObjects(...) seem (to me) to differ only by their default template arguments. I

How Does std::enable_if work?

∥☆過路亽.° 提交于 2019-11-27 08:41:39
I just asked this question: std::numeric_limits as a Condition I understand the usage where std::enable_if will define the return type of a method conditionally causing the method to fail to compile. template<typename T> typename std::enable_if<std::numeric_limits<T>::is_integer, void>::type foo(const T &bar) { isInt(bar); } What I don't understand is the second argument and the seemingly meaningless assignment to std::enable_if when it's declared as part of the template statement, as in Rapptz answer . template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>