sfinae

SFINAE differentiation between signed and unsigned

Deadly 提交于 2019-12-19 01:46:32
问题 I have functions for converting different arithmetic types to a half precision floating point type (just a uint16_t on the lowest level) and I have different functions for integer and floating point source types, using SFINAE and std::enable_if : template<typename T> uint16_t to_half(typename std::enable_if< std::is_floating_point<T>::value,T>::type value) { //float to half conversion } template<typename T> uint16_t to_half(typename std::enable_if< std::is_integral<T>::value,T>::type value) {

What does SFINAE not work correctly with following has_member function?

强颜欢笑 提交于 2019-12-19 00:45:34
问题 I'm trying out examples from Walter Brown's TMP talk and I'm trying to get his has_member implementation working. However the implementation seems to falsely return true which leads me to believe there is some detail of SFINAE that I am not understanding. #include <iostream> #include <type_traits> template <class ...> using void_t = void; template <class, class = void> struct has_type_member: std::false_type {}; template <class T> struct has_type_member<T, void_t<typename T::type> >: std:

How to avoid decay with template parameter deduction

我与影子孤独终老i 提交于 2019-12-18 19:45:11
问题 Simplified: // CHAR_TYPE == char, wchar_t, ... template <typename CHAR_TYPE, unsigned CHAR_COUNT> void Foo(CHAR_TYPE const (&value)[CHAR_COUNT]) noexcept { TRACE("const ref array"); // perform a bit of logic and forward... } template <typename CHAR_TYPE> void Foo(CHAR_TYPE const* value) noexcept { TRACE("const ptr"); // perform a bit of logic and forward... } // [ several other overloads ] Callsite: char const* ptr = ... wchar_t const* wptr = ... Foo(ptr); // <-- good: "const ptr" Foo(wptr);

Is it possible to check for existence of member templates just by an identifier?

大兔子大兔子 提交于 2019-12-18 19:32:13
问题 Can we detect member function template , variable template , class / struct / union template or alias template without knowing amount, or nature of template / non-template parameters? When I try to think about this, nothing really comes to my mind. But let's have structure with member function template: struct foo { // Really random. Let's assume we don't know this declaration, just the name "bar" template <class T, std::size_t N, class... Args> void bar(T a, T b, T(&c)[N], Args const& ...);

Checking for existence of an (overloaded) member function

夙愿已清 提交于 2019-12-18 18:01:37
问题 There are a number of answered questions about checking whether a member function exists: for example, Is it possible to write a template to check for a function's existence? But this method fails, if the function is overloaded. Here is a slightly modified code from that question's top-rated answer. #include <iostream> #include <vector> struct Hello { int helloworld(int x) { return 0; } int helloworld(std::vector<int> x) { return 0; } }; struct Generic {}; // SFINAE test template <typename T>

use sfinae to test namespace members existence

霸气de小男生 提交于 2019-12-18 17:07:52
问题 I was trying to figure out if it is possible to use sfinae to test namespace member existence. Google is rather silent about it. I've tried the following code, but it fails. namespace xyz{ struct abc{}; } struct abc{}; struct test_xyz{ typedef char yes; typedef struct{ char a[2]; } no; template <class C> static yes test(xyz::C = xyz::C()); //lets assume it has default constructor template <class C> static no test(...); const bool has_abc = sizeof(test_xyz::test<abc>()) == sizeof(yes); }; Any

Using SFINAE to check if the type is complete or not [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-18 14:35:06
问题 This question already has answers here : How to write `is_complete` template? (7 answers) Closed 5 years ago . Is it possible to check with SFINAE if the type is completely defined? E.g. template <class T> struct hash; template <> struct hash<int> {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined"); static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined"); The

Using SFINAE to check if the type is complete or not [duplicate]

邮差的信 提交于 2019-12-18 14:33:23
问题 This question already has answers here : How to write `is_complete` template? (7 answers) Closed 5 years ago . Is it possible to check with SFINAE if the type is completely defined? E.g. template <class T> struct hash; template <> struct hash<int> {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined"); static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined"); The

can I use SFINAE to selectively define a member variable in a template class?

末鹿安然 提交于 2019-12-18 11:48:39
问题 So what I want to do is to create a template class which may or may not contain a member variable based on the template argument passed in. like following: template<typename T, bool flag> class base { foov<std::enable_if<flag, T>::type> m_var; }; the above code could not survive the compiler. Does anyone know how I can achieve this? 回答1: Have a base class with enabled/disabled members based on template parameters: template<typename T, typename Enable = void> class base_class; // my favourite

What is decltype with two arguments?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 09:45:34
问题 Edit, in order to avoid confusion: decltype does not accept two arguments. See answers. The following two structs can be used to check for the existance of a member function on a type T during compile-time: // Non-templated helper struct: struct _test_has_foo { template<class T> static auto test(T* p) -> decltype(p->foo(), std::true_type()); template<class> static auto test(...) -> std::false_type; }; // Templated actual struct: template<class T> struct has_foo : decltype(_test_has_foo::test