sfinae

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

余生颓废 提交于 2020-01-02 12:13:38
问题 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

Specialize template based on whether a specific member exists

為{幸葍}努か 提交于 2020-01-02 10:09:44
问题 I want to write a trait that returns the integral type (float, int, char...) of a given type. Base is: template< class T, typename T_SFINAE = void > struct IntegralType; template< class T > struct IntegralType< T, std::enable_if< (std::is_integral<T>::value || std::is_floating_point<T>::value) > >{ using type = T; } template< class T > struct IntegralType<T>: IntegralType<T::type>{} And I want it to return double for: struct foo{ using type = double; } struct bar{ using type = foo; }

Specialize template based on whether a specific member exists

北城以北 提交于 2020-01-02 10:09:11
问题 I want to write a trait that returns the integral type (float, int, char...) of a given type. Base is: template< class T, typename T_SFINAE = void > struct IntegralType; template< class T > struct IntegralType< T, std::enable_if< (std::is_integral<T>::value || std::is_floating_point<T>::value) > >{ using type = T; } template< class T > struct IntegralType<T>: IntegralType<T::type>{} And I want it to return double for: struct foo{ using type = double; } struct bar{ using type = foo; }

How to enable member function using boolean template parameter?

*爱你&永不变心* 提交于 2020-01-02 05:39:11
问题 I would like a class to have two different implementations of push , and choose based on a boolean template argument. I tried using the SFINAE principle as described in this answer, like so: template<class T, bool foo=true> class Bar { template <> typename std::enable_if<foo>::type push(const T& value) { /* one implementation */} template <> typename std::enable_if<!foo>::type push(const T& value) { /* another implementation */ } } however, I am getting an error of "cannot specialize a

Why does the following code compile even though I have undefined member functions?

北慕城南 提交于 2020-01-02 02:12:14
问题 I was halfway through working on this piece of code and thought this is obviously not going to compile before hitting the build button. I was surprised that it not only compiled, but linked and worked as well. If I were to guess I would say that SFINAE is responsible for it compiling... is it? struct BaseClass { public: BaseClass() {} template<typename T> BaseClass(const T& a_other) { int i = 0; // for break point } template<typename T> BaseClass& operator= (const T& a_other) { int i = 0; //

How does one use enable_if for mutually exclusive non-member function templates?

喜你入骨 提交于 2020-01-01 10:04:25
问题 I'm trying to write non-member operator function templates like: #include <utility> template < typename T, unsigned L > class MyType; template < typename T, typename U, unsigned L > auto operator ==( MyType<T,L> const &l, MyType<U,L> const &r ) -> decltype( std::declval<T>() == std::declval<U>() ) { /*...*/ } But when I try to handle when l and r have different lengths: template < typename T, unsigned Lt, typename U, unsigned Lu, class Enable = typename std::enable_if<(Lt < Lu)>::type > auto

SFINAE to enable nontemplate member function

好久不见. 提交于 2020-01-01 10:04:04
问题 This is probably a duplicate, but I just can't find one where the OP clearly has the same problem I'm having. I have a class, and I'm trying to enable operator- only if the class template parameter is not an unsigned type. #include <type_traits> template<class T> struct A { typename std::enable_if<!std::is_unsigned<T>::value,A>::type operator-() {return {};} }; int main() { A<unsigned> a=a; } Unfortunately, this produces a compiler error any time I instantiate it with an unsigned type as

How to resolve ambiguity in overloaded functions using SFINAE

血红的双手。 提交于 2020-01-01 04:52:22
问题 I have an incredibly exciting library that can translate points: it should work with any point types template<class T> auto translate_point(T &p, int x, int y) -> decltype(p.x, p.y, void()) { p.x += x; p.y += y; } template<class T> auto translate_point(T &p, int x, int y) -> decltype(p[0], void()) { p[0] += x; p[1] += y; } translate_point will work with points that have public x and y members, and it will also work with tuples/indexable containers where x and y are represented by the first

SFINAE decltype comma operator trick

让人想犯罪 __ 提交于 2019-12-31 21:09:11
问题 After reading Matthieu's answer here, I decided to try this myself. My attempt fails to compile because SFINAE doesn't kick in and cull the has_foo function which attempts to access T::foo . error: ‘struct Bar’ has no member named ‘foo’ Am I missing something, or is what I'm attempting to do not possible in this way? (I'm using gcc-4.7.2) Full examplar below: #include <iostream> // culled by SFINAE if foo does not exist template<typename T> constexpr auto has_foo(T& t) -> decltype((void)t.foo

Is out-of-line sfinae on template member functions possible?

佐手、 提交于 2019-12-31 02:31:11
问题 Demo A in class declaration of A::foo. struct A { template <typename T> void foo(T a); }; A::foo is now split by sfinae. template <typename T> typename std::enable_if<(sizeof(T) > 4), void>::type A::foo(T a ) { std::cout << "> 4 \n"; } This doesn't work. Is this not allowed? 回答1: The return type in the declaration must match the definition. struct A { template <typename T> typename std::enable_if<(sizeof(T) > 4), void>::type foo(T a); }; SFINAE cannot be encapsulated as an implementation