sfinae

Template method to select between functions based on accessibility of constructor

别等时光非礼了梦想. 提交于 2019-12-03 12:30:53
I am writing a class ptr_scope_manager to manage the creation and destruction of pointers in a given scope. I have studied the answers from this question: Private constructor inhibits use of emplace[_back]() to avoid a move And it appears that if I want to manage the creation of an object whose class has a private constructor, my internal std::vector can use push_back but not emplace_back to construct the object. This is because emplace_back uses an internal class to construct the object. That means friending the ptr_scope_manager is not sufficient to allow it to create objects with private

Is substitution failure an error with dependent non-type template parameters?

允我心安 提交于 2019-12-03 12:12:35
Let's say I have these template aliases: enum class enabler {}; template <typename T> using EnableIf = typename std::enable_if<T::value, enabler>::type; template <typename T> using DisableIf = typename std::enable_if<!T::value, enabler>::type; I can do the following in GCC: #include <iostream> template <typename T, EnableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is polymorphic\n"; } template <typename T, DisableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is not polymorphic\n"; } struct foo { virtual void g() {} }; int main() { f(foo {}); f(int {}); } It prints:

sfinae with decltype: bug in clang or gcc?

冷暖自知 提交于 2019-12-03 12:05:11
Clang-3.2 can compile and code behave as expected: struct have_f { int f(int i) {return 10;} }; struct empty {}; template <class T> struct outer { T t; // if T have f(), define outer_f() template<class U=decltype(t.f(1))> int outer_f(int i) { return t.f(i); } }; int main() { outer<have_f> o1; outer<empty> o2; // to silence unused var warning return o1.outer_f(10) + sizeof(o2); } GCC of any version rejects with: t.cc:13:6: error: ‘struct empty’ has no member named ‘f’ int outer_f(int i) { return t.f(i); } ^ Who is correct? Gcc or Clang? Note, that there was similar question , without real

Multiple SFINAE class template specialisations using void_t

大城市里の小女人 提交于 2019-12-03 09:42:12
Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member typedef called "type". Here, a single specialisation is employed. This could be extended to identify say whether a type has either a member typedef called "type1", or one called "type2". The C++1z code below compiles with GCC, but not Clang. Is it legal? template <class, class = std::void_t<>> struct has_members : std::false_type {}; template

How do I determine if a type is callable with only const references?

空扰寡人 提交于 2019-12-03 09:34:39
问题 I want to write a C++ metafunction is_callable<F, Arg> that defines value to be true , if and only if the type F has the function call operator of the form SomeReturnType operator()(const Arg &) . For example, in the following case struct foo { void operator(const int &) {} }; I want is_callable<foo, int &> to be false and is_callable<foo, const int &> to be true . This is what I have so far : #include <memory> #include <iostream> template<typename F, typename Arg> struct is_callable {

Is it possible to specialize a template definition based on the existence of a nested typedef of a template type parameter?

。_饼干妹妹 提交于 2019-12-03 08:03:58
I have a template, template <typename T> class wrapper , that I would like to specialize based on the existence of typename T::context_type . If typename T::context_type is declared, then the constructors and assignment operator overloads of the wrapper<T> instantiation should accept a mandatory typename T::context_type parameter. Additionally, wrapper<T> objects would store "context" in the member data. If typename T::context_type does not exist, then the constructors and assignment operator overloads of wrapper<T> would take one less parameter and there would be no additional data member. Is

Do non-dependent default template arguments of function templates allow for SFINAE?

跟風遠走 提交于 2019-12-03 06:00:11
With "non-dependent" here I mean "non-dependent on any other template arguments of that specific function template". While answering this question , I thought I found the answer, but according to @Johannes (in the comments to my answer), I'm misinterpreting the standard here. Take the following simple example: #include <type_traits> template<class T> struct X{ template<class U = typename T::type> static void foo(int){} static void foo(...){} }; int main(){ X<std::enable_if<false>>::foo(0); } ( Live version. ) Is there any guarantee that the above compiles? GCC and Clang disagree here, as can

Using alias templates for sfinae: does the language allow it?

╄→гoц情女王★ 提交于 2019-12-03 05:38:38
问题 I have just discovered the following technique. It looks very close to one of proposed concepts syntax, works perfectly on Clang, GCC and MSVC. template <typename T, typename = typename std::enable_if<std::is_rvalue_reference<T&&>::value>::type> using require_rvalue = T&&; template <typename T> void foo(require_rvalue<T> val); I tried to find it with search requests like "sfinae in type alias" and got nothing. Is there a name for this technique and does the language actually allows it? The

Making `std::get` play nice with SFINAE

删除回忆录丶 提交于 2019-12-03 05:12:48
std::get does not seem to be SFINAE-friendly, as shown by the following test case: template <class T, class C> auto foo(C &c) -> decltype(std::get<T>(c)) { return std::get<T>(c); } template <class> void foo(...) { } int main() { std::tuple<int> tuple{42}; foo<int>(tuple); // Works fine foo<double>(tuple); // Crashes and burns } See it live on Coliru The goal is to divert the second call to foo towards the second overload. In practice, libstdc++ gives: /usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.3.0/../../../../include/c++/6.3.0/tuple:1290:14: fatal error: no matching function for call to '

void_t fails on Visual Studio 2015

孤人 提交于 2019-12-03 04:57:46
I don't understand why the following test always fails with Visual Studio 2015 (the static_assert triggers): #include <type_traits> using namespace std; template<class T> using try_assign = decltype(declval<T&>() = declval<T const&>()); template<class, class = void> struct my_is_copy_assignable : false_type {}; template<class T> struct my_is_copy_assignable<T, void_t<try_assign<T>>> : true_type {}; int main() { static_assert(my_is_copy_assignable<int>::value, "fail"); return 0; } It's basically the transcription of Walter E Brown's example usage of void_t from his cppcon 2014 presentation