sfinae

Check if a class has a possibly-overloaded function call operator

。_饼干妹妹 提交于 2020-06-27 04:11:14
问题 I am wondering whether it would be possible to implement a trait in C++20 to check if a type T is such that it has a possibly overloaded/possibly templated function call operator: operator() . // Declaration template <class T> struct has_function_call_operator; // Definition ??? // Variable template template <class T> inline constexpr bool has_function_call_operator_v = has_function_call_operator<T>::value; so that a code such as the following would lead to the correct result: #include

How does the compiler determine between a function using SFINAE and a standard function if both are viable?

戏子无情 提交于 2020-06-22 03:25:48
问题 Consider the following code: #include <iostream> #include <type_traits> template <typename T> class A { public: // Allow func to be called if T is the const version of T2 // e.g., T is 'int const' and T2 is 'int' template <typename T2, typename = typename std::enable_if< std::is_same<T, T2 const>::value>::type> void func(A<T2> const &) { std::cout << "Conversion" << std::endl; } // Allow func to be called for same version of T void func(A const &) { std::cout << "No conversion" << std::endl;

Member function template selection and SFINAE

。_饼干妹妹 提交于 2020-05-23 06:14:11
问题 I've been trying to understand the way C++ selects templates. Namely, consider the following code sample: template <typename R> class Curious { public: template <typename T, typename std::enable_if<std::is_const<T>::value, int>::type = 33> void test1() {} template <typename T, typename std::enable_if<!std::is_const<T>::value, int>::type = 33> void test1() {} template <typename T, typename = typename std::enable_if<std::is_const<T>::value>::type> void test2() {} template <typename T, typename

add function member to a template class conditionally [duplicate]

狂风中的少年 提交于 2020-05-14 08:48:30
问题 This question already has answers here : How to conditionally add a function to a class template? (5 answers) Closed last month . I have a class template defined as follow template<typename T> class A { T t_; // void f(); }; My question is how to add the f() method only if the type T is integer without compilation error. int main() { A<int> a; // OK A<string> b; // OK } Example : #include <type_traits> #include <new> #include <iostream> #include <string> template <typename T> struct Foo { T t

add function member to a template class conditionally [duplicate]

喜欢而已 提交于 2020-05-14 08:48:07
问题 This question already has answers here : How to conditionally add a function to a class template? (5 answers) Closed last month . I have a class template defined as follow template<typename T> class A { T t_; // void f(); }; My question is how to add the f() method only if the type T is integer without compilation error. int main() { A<int> a; // OK A<string> b; // OK } Example : #include <type_traits> #include <new> #include <iostream> #include <string> template <typename T> struct Foo { T t

Separate definition and declaration of template member function using enable_if whose template parameter also includes a constexpr member function

China☆狼群 提交于 2020-05-09 05:53:11
问题 The following doesn't compile under g++ 8.1.0 on CentOS 7: hey.h #pragma once #include <iostream> #include <type_traits> class Valid {}; class Invalid {}; struct Hey { template<typename T> static constexpr bool is_valid() { return std::is_same_v<T, Valid>; } template<typename T, std::enable_if_t<is_valid<T>()>* = nullptr> void howdy() const; }; template<typename T, std::enable_if_t<Hey::is_valid<T>()>*> void Hey::howdy() const { std::cout << "Howdy" << std::endl; } Compiler output: In file

SFINAE : Delete a function with the same prototype

白昼怎懂夜的黑 提交于 2020-05-07 19:00:25
问题 I wonder what is the difference between this code that works : #include <type_traits> #include <iostream> template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>; template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>; template<typename T, is_ref<T> = true> void foo(T&&) { std::cout << "ref" << std::endl; } template<typename T, is_not_ref<T> = true> void foo(T&&) { std::cout << "not ref" << std::endl; } int main() { int a = 0; foo(a)

“Function template has already been defined” with mutually exclusive `enable_if`s

只愿长相守 提交于 2020-04-07 05:15:45
问题 MSVC produces error ("function template has already been defined") for the following code: template<typename T, typename = std::enable_if_t<std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } // note difference from above ---> ! template<typename T, typename = std::enable_if_t<!std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } I thought it would work because there are mutually exclusive sfinae conditions. Can you help me with the hole in my

“Function template has already been defined” with mutually exclusive `enable_if`s

戏子无情 提交于 2020-04-07 05:12:33
问题 MSVC produces error ("function template has already been defined") for the following code: template<typename T, typename = std::enable_if_t<std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } // note difference from above ---> ! template<typename T, typename = std::enable_if_t<!std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } I thought it would work because there are mutually exclusive sfinae conditions. Can you help me with the hole in my

Passing different lambdas to function template in c++

泪湿孤枕 提交于 2020-03-01 14:49:47
问题 I have a class Foo that accepts different predicate variants through its constructor. template<typename T> struct Value { T value; }; class Foo { public: template<typename T> Foo(Value<T> &value, function<bool()> predicate) { } template<typename T> Foo(Value<T> &value, function<bool(const Value<T> &)> predicate) : Foo(value, function<bool()>([&value, predicate](){ return predicate(value); })) { } }; This allows me to construct the class with explicit function object: Value<int> i; Foo foo0(i,