enable-if

enable class's member depending on template

天大地大妈咪最大 提交于 2019-12-03 12:30:19
I already know that you can enable (or not) a class's method using std::enable_if for exemple: template<size_t D, size_t E> class Field { ... size_t offset(const std::array<float,D>& p) const { ... } template<typename TT = size_t> typename std::enable_if<D!=E, TT>::type offset(const std::array<float,E>& p) const { return offset(_projection(p)); } ... }; This helps not being able to call function that are invalid in a specific case as well as removing overloading errors ... which, to me, is very nice ! I'd like to go further and make some of my class's members being present only if the are

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

左心房为你撑大大i 提交于 2019-12-03 02:15:29
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 << "integral" << std::endl; return; } //This function is chosen when a floating point type is passed in

How to properly use std::enable_if on a constructor

懵懂的女人 提交于 2019-12-02 14:35:58
问题 This question combines several pieces of code and is a bit complicated, but I tried slimming it down as much as possible. I am trying to use std::enable_if to conditionally invoke the correct constructor as a result of ambiguous function signatures when a lambda expression is used as input, but the parameters of said lambda expression can be implicitly convertible to one another. This is an attempt to build upon the following question: Here, but is sufficiently different and focuses on std:

How to properly use std::enable_if on a constructor

狂风中的少年 提交于 2019-12-02 11:53:55
This question combines several pieces of code and is a bit complicated, but I tried slimming it down as much as possible. I am trying to use std::enable_if to conditionally invoke the correct constructor as a result of ambiguous function signatures when a lambda expression is used as input, but the parameters of said lambda expression can be implicitly convertible to one another. This is an attempt to build upon the following question: Here , but is sufficiently different and focuses on std::enable_if to merit another question. I am also providing the Live Example that works with the problem

How does std::enabled_if work when enabling via a parameter

∥☆過路亽.° 提交于 2019-12-02 06:07:02
I'm trying to understand how enable_if works and I understand almost everything except scenario #3 from https://en.cppreference.com/w/cpp/types/enable_if template<class T> void destroy(T* t, typename std::enable_if<std::is_trivially_destructible<T>::value>::type* = 0) { std::cout << "destroying trivially destructible T\n"; } if the expression in enable_if is true then partial template specialization is chosen, so if it is chosen: why in enable_if is only condition without indicating second template parameter ? What type is "type*" then ? void* ? if so, why ? Why is it pointer ? why in enable

What's the use of second parameter of std::enable_if?

∥☆過路亽.° 提交于 2019-12-02 04:44:57
问题 I am confused about the second parameter of std::enable_if. In using of a return type of int, we can make it using: template <class T> typename std::enable_if<mpi::is_builtin<T>::value, int>::type foo() { return 1; } But how can I use enable_if in paramter or template? In this case, what's the difference of too functions below: template<class T , class = typename std::enable_if<std::is_integral<T>::value>::type > T too(T t) { std::cout << "here" << std::endl; return t; } int too(int t) { std:

enable_if with copy constructors

笑着哭i 提交于 2019-12-01 17:27:40
问题 I am trying std::enable_if for the first time and struggling. Any guidance would be appreciated. As a toy example, here is a simple static vector class, for which I want to define a copy constructor, but the behaviour depends on the relative sizes of the vectors: just copy data into a smaller or same-sized vector copy data into a larger vector and then pad the rest with zeroes So the vector class is: template <size_t _Size> class Vector { double _data[_Size]; public: Vector() { std::fill(

Can I use boost::enable_if on a member function?

点点圈 提交于 2019-12-01 16:50:29
I'm writing a template class, and I want to allow an additional method to exist only for a certain template type. Currently the method exists for all template types, but causes a compilation error for all other types. Complicating this is that it's an overloaded operator(). Not sure if what I want to do is actually possible here. Here's what I have now: template<typename T, typename BASE> class MyClass : public BASE { public: typename T& operator() (const Utility1<BASE>& foo); typename T const& operator() (const Utility2<BASE>& foo) const; }; I want the T& version always available, but the T

Can I use boost::enable_if on a member function?

纵饮孤独 提交于 2019-12-01 15:45:35
问题 I'm writing a template class, and I want to allow an additional method to exist only for a certain template type. Currently the method exists for all template types, but causes a compilation error for all other types. Complicating this is that it's an overloaded operator(). Not sure if what I want to do is actually possible here. Here's what I have now: template<typename T, typename BASE> class MyClass : public BASE { public: typename T& operator() (const Utility1<BASE>& foo); typename T

Recursively dereference pointer

你说的曾经没有我的故事 提交于 2019-12-01 11:18:24
问题 While trying to answer one question here, I found this question: How to recursively dereference pointer (C++03)? Adapted code from the answer is following: template<typename T> T& dereference(T &v) { return v; } template<typename T> const T& dereference(const T &v) { return v; } template <typename T> typename std::enable_if<!std::is_pointer<T>::value, T&>::type dereference(T *v) { return dereference(*v); } However, in this test it is failing to dereference pointer-to-pointer into the value