sfinae

Private member access in template substitution and SFINAE

感情迁移 提交于 2021-02-08 12:20:57
问题 class A { int a; }; template<typename, typename = void> class test {}; template<typename T> class test<T,decltype(T::a)> {}; int main() { test<A> a; } The code above compiles without error on clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final) , but fails to compile on g++-5 (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904 and g++-6 (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901 with errors like this: main.cpp: In function ‘int main()’: main.cpp:9:22: error: ‘int A::a’ is private within this

Private member access in template substitution and SFINAE

橙三吉。 提交于 2021-02-08 12:19:11
问题 class A { int a; }; template<typename, typename = void> class test {}; template<typename T> class test<T,decltype(T::a)> {}; int main() { test<A> a; } The code above compiles without error on clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final) , but fails to compile on g++-5 (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904 and g++-6 (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901 with errors like this: main.cpp: In function ‘int main()’: main.cpp:9:22: error: ‘int A::a’ is private within this

c++ Use std::enable_if to conditionally add getters to a variadic variant template

可紊 提交于 2021-02-08 09:12:22
问题 I am trying to add specializations for the case where my variant has any of int , float , bool , and others as template arguments. My attempt so far is: #include <iostream> #include <variant> #include <string> #include <type_traits> template<typename... Types> struct variant : std::variant<Types...> { using std::variant<Types...>::variant; template<typename T> const T& get() const { return std::get<T>(*this); } #define VARGET(X) typename std::enable_if<(std::is_same<Types, X>::value || ... ),

Passing result of std::bind to std::function “overloads”

微笑、不失礼 提交于 2021-02-08 05:17:42
问题 I have problem similar to Passing different lambdas to function template in c++ but now with wrappers created by std::bind instead of lambdas. I have two overloads of method Add that take different forms of std::function : template<typename T> struct Value { T value; }; template <typename T> void Add(Value<T> &value, function<bool()> predicate) { } template <typename T> void Add(Value<T> &value, block_deduction<function<bool(const Value<T> &)>> predicate) { } This now works fine with lambdas

Add method to class by template parameter

女生的网名这么多〃 提交于 2021-02-07 22:51:12
问题 I would like to have a template parameter specific function inside a class unsing enable_if. Its name stays the same, the parameter type varies (although this should not be relevant since only one is initialized). enum class MyCases { CASE1, CASE2 }; template<enum MyCases case> class MyClass { template<typename = typename std::enable_if<case == MyCases::CASE1>::type> void myFunction(ParameterTypeA a) { ... } template<typename = typename std::enable_if<case == MyCases::CASE2>::type> void

Add method to class by template parameter

戏子无情 提交于 2021-02-07 22:51:08
问题 I would like to have a template parameter specific function inside a class unsing enable_if. Its name stays the same, the parameter type varies (although this should not be relevant since only one is initialized). enum class MyCases { CASE1, CASE2 }; template<enum MyCases case> class MyClass { template<typename = typename std::enable_if<case == MyCases::CASE1>::type> void myFunction(ParameterTypeA a) { ... } template<typename = typename std::enable_if<case == MyCases::CASE2>::type> void

Add method to class by template parameter

帅比萌擦擦* 提交于 2021-02-07 22:50:34
问题 I would like to have a template parameter specific function inside a class unsing enable_if. Its name stays the same, the parameter type varies (although this should not be relevant since only one is initialized). enum class MyCases { CASE1, CASE2 }; template<enum MyCases case> class MyClass { template<typename = typename std::enable_if<case == MyCases::CASE1>::type> void myFunction(ParameterTypeA a) { ... } template<typename = typename std::enable_if<case == MyCases::CASE2>::type> void

Ambiguous template with SFINAE dummy parameter

夙愿已清 提交于 2021-02-07 02:54:49
问题 Consider a case where one needs to verify a type T with another template g (could be some enable_if expression, for example) inside a dummy parameter of another template, like this: template<class> struct g { typedef void type; }; template<class, class> struct f {}; template<class T> struct f<T, void> {}; // Case A template<class T> struct f<T*, typename g<T>::type> {}; // Case B int main() { f<int*, void> test; } Here, for the sake of simplicity g doesn't really do anything. The second

Ambiguous template with SFINAE dummy parameter

梦想与她 提交于 2021-02-07 02:54:49
问题 Consider a case where one needs to verify a type T with another template g (could be some enable_if expression, for example) inside a dummy parameter of another template, like this: template<class> struct g { typedef void type; }; template<class, class> struct f {}; template<class T> struct f<T, void> {}; // Case A template<class T> struct f<T*, typename g<T>::type> {}; // Case B int main() { f<int*, void> test; } Here, for the sake of simplicity g doesn't really do anything. The second

How to check for the existence of a subscript operator?

南笙酒味 提交于 2021-02-06 18:47:07
问题 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