sfinae

How to check for the existence of a subscript operator?

落爺英雄遲暮 提交于 2021-02-06 18:44:43
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

How to check for the existence of a subscript operator?

早过忘川 提交于 2021-02-06 18:40:20
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

static_assert if expressions is constexpr

不问归期 提交于 2021-02-06 09:44:29
问题 I want to create a class template template <class T> class X { // here I'll use T::value (among other things) }; T::value will often be a constexpr static variable, but not always. T::value has to be positive value, so I want to let people know it during compilation, when possible. If T::value was always constexpr, I'd add static_assert like static_assert(T::value > 0, "need positive number"); Is it possible to add this static_assert only for cases when T::value is constexpr? 回答1: We can

Narrowing int to bool in SFINAE, different output between gcc and clang

烂漫一生 提交于 2021-02-04 22:12:04
问题 Consider the following example: template<int i> struct nice_type; template<class T> struct is_nice : std::false_type {}; template<int i> struct is_nice< nice_type<i> > : std::integral_constant<int, i> {}; template<class T, class = void> struct pick { typedef std::integral_constant<int, -1> type; }; template<class T> struct pick<T, typename std::enable_if< is_nice<T>::value >::type > { typedef std::integral_constant<int, is_nice<T>::value > type; }; int main() { std::cout << pick<int>::type:

enable_if function defined when it shouldn't be

风流意气都作罢 提交于 2021-02-04 16:41:09
问题 As an experiment, I am trying to make a void member function with no parameters change behavior based on the class template parameter: #include <iostream> #include <limits> template<typename T> class MyClass { public: void MyFunc(const typename std::enable_if<std::is_fundamental<T>::value, T> dummy = T()); void MyFunc(const typename std::enable_if<!std::is_fundamental<T>::value, T> dummy = T()); }; template<typename T> void MyClass<T>::MyFunc(const typename std::enable_if<std::is_fundamental

Why does this SFINAE not work with enable_if when one conditional branch is inherited from the base class?

江枫思渺然 提交于 2021-01-28 06:25:30
问题 #include <bits/stdc++.h> #include <type_traits> // Type your code here, or load an example. template <typename Types> class C1 { public: using A=typename Types::A; using B=typename Types::B; template <typename Dummy = void> inline typename std::enable_if<std::is_same<A, B>::value, Dummy>::type f() { } }; template <typename Types> class C2 : public C1<Types> { public: using A=typename Types::A; using B=typename Types::B; template <typename Dummy = void> inline typename std::enable_if<!std::is

SFINAE not working to conditionally compile member function template

旧街凉风 提交于 2021-01-27 23:24:37
问题 I'm trying you use std::enable_if to conditionally choose only one out of two member function template using SFINAE with this code: #include <iostream> #include <type_traits> template<typename T> struct C { template<typename Q = T, typename = typename std::enable_if<std::is_same<Q, int>::value>::type> int foo() { return 1; } template<typename Q = T, typename = typename std::enable_if<!std::is_same<Q, int>::value>::type> int foo() { return 0; } }; int main() { std::cout << C<int>().foo() <<

SFINAE inside concept template argument

我的梦境 提交于 2021-01-27 06:29:08
问题 Does SFINAE work inside a concept argument? (maybe it's not called SFINAE here). Example: template <class F> requires std::invocable<F, int> && // <-- is this needed? (!std::same_as<std::invoke_result_t<F, int>, void>) auto foo(F f, int a) -> int Is the above std::invocable<F, int> required? If we omit it like so: template <class F> requires (!std::same_as<std::invoke_result_t<F, int>, void>) auto foo(F f, int a) -> int Is this version well-formed even if std::invoke_result_t<F, int> is not

Difference between SFINAE and tag dispatch

为君一笑 提交于 2021-01-23 06:38:04
问题 In this video https://youtu.be/Vkck4EU2lOU?t=582 "tag dispatch" and SFINAE are being presented as the alternatives, allowing to achieve selection of the desired template function. Is it correct? Isn't "tag dispatch" using SFINAE? If it's correct, what is the difference between SFINAE and tag dispatch exactly? 回答1: Tag dispatch takes advantage of overload resolution to select the right overload. auto f_impl(std::true_type) { return true; } auto f_impl(std::false_type) { return std::string("No"

why template parameter which is explicitely given can not be “deduced”

时光总嘲笑我的痴心妄想 提交于 2021-01-05 08:55:42
问题 Coming from that question: Using enum values in combination with SFINAE I tried to implement: enum Specifier { One, Two, Three }; template <Specifier, typename UNUSED=void> struct Foo { void Bar(){ std::cout << "Bar default" << std::endl;} }; template <Specifier s , typename std::enable_if<s == Specifier::Two || s == Specifier::One, int>::type> struct Foo<s> { void Bar(){ std::cout << "Bar Two" << std::endl; } }; int main() { Foo< One >().Bar(); Foo< Two >().Bar(); } Fails with: > main.cpp