enable-if

enable_if using a constexpr bool test not working

那年仲夏 提交于 2019-12-11 03:24:00
问题 I have a maths function that I want to be able to accept either a double, or a array/vector/container of doubles, and behave slightly differently. I am attempting to use SFINAE and type traits to select the correct function. Here is a minimal example: #include <iostream> #include <vector> #include <type_traits> template <typename T> constexpr bool Iscontainer() { if constexpr (std::is_class<T>::value && std::is_arithmetic<typename T::value_type>::value) { return true; } return false; } //

usage of enable_if for non-templated member function [duplicate]

▼魔方 西西 提交于 2019-12-11 01:10:03
问题 This question already has answers here : std::enable_if to conditionally compile a member function (6 answers) Closed 6 years ago . The book C++ Programming Language(fourth edition). Chapter 28.4 (page 796) explains enable_if and gives an example of making the definition of operator->() conditional. The example in the book is only a code snippet, and I completed it to a program as follows: #include <iostream> #include <type_traits> #include <complex> using namespace std; template<bool B,

Why Must Specializing Argument be void?

Deadly 提交于 2019-12-10 15:26:58
问题 So yet another question in this saga. Guillaume Racicot has been good enough to provide me with yet another workaround so this is the code I'll be basing this question off of: struct vec { double x; double y; double z; }; namespace details { template <typename T> using subscript_function = double(*)(const T&); template <typename T> constexpr double X(const T& param) { return param.x; } template <typename T> constexpr double Y(const T& param) { return param.y; } template <typename T> constexpr

Check if type is declared as a meta type system (for SFINAE)

别来无恙 提交于 2019-12-10 13:59:57
问题 To make a case distinction for a parameter t of type T using SFINAE, I want to know if the statement QVariant::fromValue(t); and / or QVariant::value<T>(); compiles. If the one compiles, the other one does too, unless you hack the meta type system. They compile if and only if T has been declared using Q_DECLARE_METATYPE(T) . Very simple usage example, where one wants to print the type of a value by simply qDebugging a variant-wrapped equivalent, if and only if supported by the meta type

Using std::enable_if with anonymous type parameters

烂漫一生 提交于 2019-12-09 03:13:51
问题 I try to use std::enable_if with an unused and unnamed type parameter, in order to not distort the return type. However, the following code does not compile. #include <iostream> template <typename T, typename = std::enable_if_t<!std::is_integral<T>::value>> T foo() { std::cout << "non-integral" << std::endl; return T(); } template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>> T foo() { std::cout << "integral" << std::endl; return T(); } int main() { foo<float>(); foo

why compiler said: 'enable_if' cannot be used to disable this declaration

*爱你&永不变心* 提交于 2019-12-08 21:23:09
问题 template <bool Cond, typename Type = void> using Enable_if = typename std::enable_if<Cond, Type>::type; class Degree; template <typename T> constexpr inline bool Is_Degree() { return std::is_base_of<Degree, T>::value; } class Degree { public: std::size_t inDeg = 0; }; template <typename Satellite = Degree> class Vertex: public Satellite { public: explicit Vertex(int num): n(num) {} private: std::size_t n; }; template <typename Satellite = Degree> class Edge { public: // i want have different

Using C++11 std::enable_if to enable member function if vector is specific length

混江龙づ霸主 提交于 2019-12-08 16:37:41
问题 I am writing a simple vector class and I would like to have some member functions that are only available in vectors of certain lengths (cross product for a 3 element vector for example). I stumbled across std::enable_if and it looks like it may be able to do what I want, but I don't seem to be able to get it working correctly. #include <iostream> #include <type_traits> template<typename T, unsigned int L> class Vector { private: T data[L]; public: Vector<T,L>(void) { for(unsigned int i = 0;

Why does std::is_rvalue_reference not do what it is advertised to do?

倖福魔咒の 提交于 2019-12-08 16:31:40
问题 For example if I have #include <type_traits> struct OwnershipReceiver { template <typename T, class = typename std::enable_if < !std::is_lvalue_reference<T>::value >::type > void receive_ownership(T&& t) { // taking file descriptor of t, and clear t } }; copied from How to make template rvalue reference parameter ONLY bind to rvalue reference? the poster uses !std::is_lvalue_reference instead of the immediately more obvious std::is_rvalue_reference . I've verified this in my own code where

Group class template specializations

﹥>﹥吖頭↗ 提交于 2019-12-08 15:21:17
问题 Is there a technique / best style to group class template specializations for certain types ? An example : Lets say I have a class template Foo and I need to have it specialized the same for the typeset A = { Line, Ray } and in another way for the typeset B B = { Linestring, Curve } What I'm doing so far : (the technique is also presented here for functions) #include <iostream> #include <type_traits> using namespace std; // 1st group struct Line {}; struct Ray {}; // 2nd group struct Curve {}

C++ boost enable_if question

让人想犯罪 __ 提交于 2019-12-07 16:32:36
问题 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