sfinae

does sfinae instantiates a function body?

此生再无相见时 提交于 2019-12-10 17:28:43
问题 I want to detect existence of a specific member function for a class, using the usual SFINAE trick. template<typename T> struct has_alloc { template<typename U,U x> struct dummy; template<typename U> static char test(dummy<void* (U::*)(std::size_t),&U::allocate>*); template<typename U> static char (&test(...))[2]; static bool const value = sizeof(test<T>(0)) ==1; }; It should be noted that this detects a different kind of allocator which has void* allocate(std::size_t) as member function

Specializing std::hash for derived classes works in gcc, not clang

六月ゝ 毕业季﹏ 提交于 2019-12-10 15:18:11
问题 I am trying to specialize std::hash for derved classes. The best approach so far is based on this answer: #include <type_traits> #include <functional> #include <unordered_set> namespace foo { template<class T, class E> using first = T; struct hashable {}; struct bar : public hashable {}; } namespace std { template <typename T> struct hash<foo::first<T, std::enable_if_t<std::is_base_of<foo::hashable, T>::value>>> { size_t operator()(const T& x) const { return 13; } }; } int main() { std:

Difference between decltype (…, void()) and void_t

对着背影说爱祢 提交于 2019-12-10 14:40:40
问题 Last time I'm founding many answers regarding SFINAE which suggest using void_t helper. But I don't seem to understand what's so special about it in regard to: decltype (..., void()). Consider the example: template <typename...> using void_t = void; template <typename T, typename = void> struct has_foo : std::false_type {}; template <typename T> struct has_foo <T, decltype (T().foo(), void())> : std::true_type {}; template <typename T, typename = void> struct has_bar : std::false_type {};

SFINAE with std::enable_if and std::is_default_constructible for incomplete type in libc++

梦想的初衷 提交于 2019-12-10 14:28:03
问题 I just observed a strange issue with libc++ when using SFINAE to detect if a templated type is default constructible. The following is a minimal example I was able to come up with: #include <iostream> #include <type_traits> template <typename T> struct Dummy; template <> struct Dummy<int>{}; template <typename T, typename = void> struct has_dummy : std::false_type {}; template <typename T> struct has_dummy<C, std::enable_if_t<std::is_default_constructible<Dummy<T>>::value>> : std::true_type{}

Check if type is declared as a meta type system (for SFINAE)

别来无恙 提交于 2019-12-10 13:59:57
问题 To make a case distinction for a parameter t of type T using SFINAE, I want to know if the statement QVariant::fromValue(t); and / or QVariant::value<T>(); compiles. If the one compiles, the other one does too, unless you hack the meta type system. They compile if and only if T has been declared using Q_DECLARE_METATYPE(T) . Very simple usage example, where one wants to print the type of a value by simply qDebugging a variant-wrapped equivalent, if and only if supported by the meta type

Using SFINAE gives different results on GCC and Clang

邮差的信 提交于 2019-12-10 13:29:46
问题 I'm learning how to use SFINAE to my advantage. I'm trying to use it to select the function implementation based on existence of a serialize() function in an object. This is the code I use to determine, if the type defines the serialize() function: template <typename T> class HasSerialize { private: typedef char yes[1]; typedef char no[2]; template <typename C> static yes& test(char[sizeof(&C::serialize)]) ; template <typename C> static no& test(...); public: static const bool value = sizeof

Is expression inside decltype executed, or just being checked for validation?

半腔热情 提交于 2019-12-10 13:27:22
问题 By using Expression SFINAE, you can detect if some operator or operation is supported or not. for example, template <class T> auto f(T& t, size_t n) -> decltype(t.reserve(n), void()) { t.reserve(n); } My question is that t.reserve(n) inside decltype get executed or not? If yes, does that mean t.reserve(n) got executed twice, one inside decltype and the other one inside the function body? If not, is it just checked for validation during compilation time? But why it is not executed, I thought

SFINAE for detecting existence of non-member template function

柔情痞子 提交于 2019-12-10 13:10:38
问题 TL;DR I want to write a template function Process(T value) that behaves differently for different values depending on the existence of a non-member function CreateProcessor<T>() . What can I do for that? I have a problem with SFINAE. Suppose we need to support function CreateProcessor that returns an implementation of interface IProcessor<T> for some type type T . In C++ we can't create several overloads of a function that differ only in return type, so we have to make function

Conflict between perfect forwarding constructor and copy constructor in class hierarchy

拈花ヽ惹草 提交于 2019-12-10 12:58:16
问题 I recently encountered a problem while trying to implement a class hierarchy with perfect forwarding constructors. Consider the following example: struct TestBase { template<typename T> explicit TestBase(T&& t) : s(std::forward<T>(t)) {} // Compiler refers to this line in the error message TestBase(const TestBase& other) : s(other.s) {} std::string s; }; struct Test : public TestBase { template<typename T> explicit Test(T&& t) : TestBase(std::forward<T>(t)) {} Test(const Test& other) :

Resolve overload ambiguity with SFINAE

雨燕双飞 提交于 2019-12-10 10:36:52
问题 I've found similar cases, but they usually ended up doing something along the lines of what I (think) I'm doing here. I want to be able to call a function with one or more parameters, obviously, if the function exists with overloads with multiple parameters, the correct version cannot be deduced without help. As I am specifying the number of arguments as well, I figured this would be enough information for the compiler to deduce the correct overload. This doesn't seem to be the case and I