sfinae

SFINAE away a copy constructor

蹲街弑〆低调 提交于 2019-12-06 19:22:57
问题 Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I pass as class template parameters. The problem is, that SFINAE only works on templates and a copy constructor/assignment operator can't be a template. Does there exist a workaround? 回答1: This solution uses a base class that is conditionally not

Boost.Hana: How to check if function has specialisation for a certain type?

依然范特西╮ 提交于 2019-12-06 12:23:06
问题 I have a template function that has no definition by default but it specialised by some types: template <typename T> auto foo(bar &, const T &) -> void; template <> auto foo<std::string>(bar &, const std::string &) -> void {} How do I write a constexpr function that tells me if type T has a specialisation for the above function? My best effort: namespace detail { auto has_foo(hana::is_valid([](auto &b, const auto &t) -> decltype(foo(b, t)) {})); } // namespace detail template <typename T>

Resolve overload ambiguity with SFINAE

落爺英雄遲暮 提交于 2019-12-06 11:17:04
I've found similar cases, but they usually ended up doing something along the lines of what I (think) I'm doing here. I want to be able to call a function with one or more parameters, obviously, if the function exists with overloads with multiple parameters, the correct version cannot be deduced without help. As I am specifying the number of arguments as well, I figured this would be enough information for the compiler to deduce the correct overload. This doesn't seem to be the case and I hope you may be able to show me why. the code: http://coliru.stacked-crooked.com/a/5e6fd8d5418eee3c

Function Overloading Based on Arbitrary Properties of Types doesn't work

China☆狼群 提交于 2019-12-06 10:52:15
In the example below, I need to extract some values. I have an efficient extractor, which can work with builtin types, and an inefficient template that can work with everything. To choose between these, I want to use Function Overloading Based on Arbitrary Properties of Types . Here is my code: #include <string> #include <iostream> class extractor { public: static void extract(const bool& val) { std::cout << "Specialized extractor called" << std::endl; } static void extract(const double& val) { std::cout << "Specialized extractor called" << std::endl; } }; template <typename T> void extract

method compile time assertion; still not working

不问归期 提交于 2019-12-06 09:35:45
问题 I need a easy way to assert inside a template that a template parameter implements a method ( or one of its parent classes ). I've read Concept check library but is hard to find an easy example to do simple checks like this one. I've tried to follow other posts (like this one and this other one), which i've modified so i can make it generic for many method types (in my example Foo (methodName) and has_foo (Checker name) will, once working correctly, be wrapped as macro arguments so it can be

Check if member function exists and is not inherited for SFINAE

情到浓时终转凉″ 提交于 2019-12-06 09:21:04
问题 How can I check if a member function exists and is not inherited? I need this to resolve ambiguity for the following example: A type either has a foo() or a bar() member function. Caller will call the one that exists for the given type. However, DerivedWithBar inherits foo() from BaseWithFoo but defines its own bar() . Thus, Caller does not know which function to call. I'd need a way to give the non-inherited foo precedence over the inherited bar() , but I do not know how to check if a member

Specialize template based on whether a specific member exists

无人久伴 提交于 2019-12-06 08:26:24
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; } IntegralType<double>::type == double IntegralType<foo>::type == double IntegralType<bar>::type == double This

how to SFINAE for enabling member function returning `auto`

假如想象 提交于 2019-12-06 08:13:50
问题 In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e. template<int N> struct A { int sum() const noexcept { return _sum<N-1>(); } private: int _data[N]; template<int I> typename std::enable_if< I,int>::type _sum() const noexcept { return _sum<I-1>() + _data[I]; } template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept { return _data[I]; } }; However, this won't work if the function in question ( _sum() in

How to use SFINAE to select constructor from multiple options in C++11

人走茶凉 提交于 2019-12-06 07:16:32
My question is an extension of this question: How to use sfinae for selecting constructors? In the previous question, the asker just wanted to selectively enable a single constructor. I would like to change the behaviour of the constructor depending on whether the class template parameter type is default-constructible - the best way that I can come up with to do this is to have two constructors with the same usage such that exactly one is enabled for every instantiation. My case is also different because neither version of the constructor would be a template function if I weren't trying to

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