enable-if

Why doesn't SFINAE (enable_if) work for member functions of a class template?

放肆的年华 提交于 2019-12-17 07:29:38
问题 #include <type_traits> struct A{}; struct B{}; template <typename T> struct Foo { typename std::enable_if<std::is_same<T, A>::value>::type bar() {} typename std::enable_if<std::is_same<T, B>::value>::type bar() {} }; Error message: 14:5: error: 'typename std::enable_if<std::is_same<T, B>::value>::type Foo<T>::bar()' cannot be overloaded 10:5: error: with 'typename std::enable_if<std::is_same<T, A>::value>::type Foo<T>::bar()' Source on cpp.sh. I thought both typename std::enable_if<std::is

Disambiguate class-member in multiple inheritance

送分小仙女□ 提交于 2019-12-17 04:35:19
问题 Suppose I have this variadic base class-template: template <typename ... Types> class Base { public: // The member foo() can only be called when its template // parameter is contained within the Types ... pack. template <typename T> typename std::enable_if<Contains<T, Types ...>::value>::type foo() { std::cout << "Base::foo()\n"; } }; The foo() member can only be called when its template-parameter matches at least one of the parameters of Base (the implementation of Contains is listed at the

C# Enable/Disable multiple textbox based on combobox selection

旧城冷巷雨未停 提交于 2019-12-13 11:08:02
问题 I am just an ordinary guy who is learning how to code. I don't like when code looks sloppy and can be consolidated. With that being said I need to consolidate some code, but I have failed to find information on how to do it. I am trying to enable/disable multiple textbox based of combobox selection. Here is the long and ugly code I am trying to consolidate private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { int Combobox_Process_Selected_Index = comboBox2.SelectedIndex;

boost:enable_if to define a dedicated method in a templated class

雨燕双飞 提交于 2019-12-13 07:40:34
问题 I would like to have a custom method - I will call MyMethod - in a templated class - I will call Foo - ONLY when Foo has been instanciated with certain template parameters types (eg. when A is int and B is string), otherwise, I don't want MyMethod to exist at all on any other possible Foo instance. Is that possible ? Example: template<class A, class B> class Foo { string MyMethod(whatever...); } boost:enable_if can help there ? Thanks!! 回答1: What you really want here is to specialize your

Using enable_if and underlying_type in function signature in VS2012

邮差的信 提交于 2019-12-13 03:55:34
问题 This code works in VS2013 and other compilers (tested clang 3.4 and gcc 4.8) but fails to compile in VS2012: #include <type_traits> #include <cstdio> // error C4519: default template arguments are only allowed on a class template template<typename E, typename std::enable_if<std::is_enum<E>::value>::type* = nullptr> typename std::underlying_type<E>::type to_integral(E e) { return static_cast<typename std::underlying_type<E>::type>(e); } template<typename E, typename std::enable_if<!std::is

Why 'enable_if' cannot be used to disable this declaration here

瘦欲@ 提交于 2019-12-12 13:05:51
问题 #include<string> #include<type_traits> template<typename... Args> class C { public: void foo(Args&&... args) { } template<typename = std::enable_if_t<(0 < sizeof...(Args))>> void foo(const Args&... args) { } }; int main() { C<> c; c.foo(); return 0; } Above code works as expacted (by me :)) and calls void foo(Args&&... args) at run-time in msvc 2015 but same code fails to even compile in both gcc 7.3 and clang 6.0.0 with error: error: no type named 'type' in 'std::enable_if'; 'enable_if'

How can I write a function template for all types with a particular type trait?

杀马特。学长 韩版系。学妹 提交于 2019-12-12 08:47:13
问题 Consider the following example: struct Scanner { template <typename T> T get(); }; template <> string Scanner::get() { return string("string"); } template <> int Scanner::get() { return 10; } int main() { Scanner scanner; string s = scanner.get<string>(); int i = scanner.get<int>(); } The Scanner class is used to extract tokens from some source. The above code works fine, but fails when I try to get other integral types like a char or an unsigned int . The code to read these types is exactly

template method matching derived type instead of base

帅比萌擦擦* 提交于 2019-12-12 01:20:06
问题 I have a set of operators that I need to override for expression templating. I would like all derived classes of a base type match to the base type. Other things would then be caught by a generic type. Unfortunately, the generic type grabs the derived types before the base type does. To make things nice and confusing, everything is templated pretty heavily, including some CRTP. Let me try to give a more simple version of the code: // Note: 'R' is used for return type template <typename

SFINAE and std::numeric_limits

大兔子大兔子 提交于 2019-12-11 15:40:36
问题 I am trying to write a stream class that handles numerical and non-numerical data separately. Can someone explain to me why this code does not compile? #include <iostream> #include <cstdlib> #include <type_traits> #include <limits> class Stream { public: Stream() {}; template<typename T, typename std::enable_if_t<std::numeric_limits<T>::is_integer::value>> Stream& operator<<(const T& val) { std::cout << "I am an integer type" << std::endl; return *this; }; template<typename T, typename std:

Compiler error with a fold expression in enable_if_t

╄→гoц情女王★ 提交于 2019-12-11 06:47:31
问题 I have the following code, where I am using a fold expression to evaluate whether all pack parameters are convertible to the first function argument. For some reason it fails to compile on msvc when I make what seems like a very trivial change: #include <type_traits> #define TRY 1 #if TRY == 1 template<typename B, typename... Args, std::enable_if_t<((std::is_convertible_v<Args&, B&> && ...)), bool> = true> void fn(B b, Args...args) {} #else template<typename B, typename... Args, typename =