sfinae

Partial specialization of a method in a templated class

百般思念 提交于 2019-11-27 03:53:31
问题 Given: struct A { virtual bool what() = 0; }; template<typename T, typename Q> struct B : public A { virtual bool what(); }; I want to partially specialize what like: template<typename T, typename Q> bool B<T, Q>::what() { return true; } template<typename Q> bool B<float, Q>::what() { return false; } But it appears that this isn't possible (is it in C++11?) so I tried SFINAE: template<typename T> typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what() { return true; }

“What happened to my SFINAE” redux: conditional template class members?

房东的猫 提交于 2019-11-27 02:59:20
问题 I'm new to writing template metaprogramming code (vs. just reading it). So I'm running afoul of some noob issues. One of which is pretty well summarized by this non-SO post called "What happened to my SFINAE?", which I will C++11-ize as this: (Note: I gave the methods different names only to help with my error diagnosis in this "thought experiment" example. See @R.MartinhoFernandes's notes on why you wouldn't actually choose this approach in practice for non-overloads.) #include <type_traits>

Assert that code does NOT compile

拈花ヽ惹草 提交于 2019-11-27 02:46:09
问题 In short: How to write a test, that checks that my class is not copyable or copy-assignable, but is only moveable and move-assignable? In general: How to write a test, that makes sure that a specific code does not compile? Like this: // Movable, but non-copyable class struct A { A(const A&) = delete; A(A&&) {} }; void DoCopy() { A a1; A a2 = a1; } void DoMove() { A a1; A a2 = std::move(a1); } void main() { // How to define these checks? if (COMPILES(DoMove)) std::cout << "Passed" << std::endl

Why compile error with enable_if

一世执手 提交于 2019-11-27 02:04:29
问题 Why this does not compile with gcc48 and clang32? #include <type_traits> template <int N> struct S { template<class T> typename std::enable_if<N==1, int>::type f(T t) {return 1;}; template<class T> typename std::enable_if<N!=1, int>::type f(T t) {return 2;}; }; int main() { S<1> s1; return s1.f(99); } GCC error: /home/lvv/p/sto/test/t.cc:12:2: error: no type named ‘type’ in ‘struct enable_if<false, int>’ f(T t) {return 2;}; ^ CLANG error: /home/lvv/p/sto/test/t.cc:11:26: error: no type named

How to use sfinae for selecting constructors?

孤人 提交于 2019-11-27 01:49:22
In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e. template<int N> struct A { int sum() const noexcept { return _sum<N-1>(); } private: int _data[N]; template<int I> typename std::enable_if< I,int>::type _sum() const noexcept { return _sum<I-1>() + _data[I]; } template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept { return _data[I]; } }; However, this doesn't work on constructors. Suppose, I want to declare the constructor template<int N> struct A { /* ... */ template<int otherN> explicit(A<otherN> const&)

If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

。_饼干妹妹 提交于 2019-11-27 00:44:49
问题 In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take the address of an overloaded function and it can not be resolved, is that failure in the immediate-context of deduction? (i.e is it a hard error or SFINAE if it can not be resolved)? Here is some sample code: struct X { // template<class T> T* foo(T,T)

SFINAE tried with bool gives compiler error: “template argument ‘T::value’ involves template parameter” [duplicate]

为君一笑 提交于 2019-11-26 23:21:04
问题 This question already has an answer here: Why is it disallowed for partial specialization in a non-type argument to use nested template parameters 2 answers I tried to implement an SFINAE using bool (unlike popular void_ trick): template<typename T, bool = true> struct Resolve { static const bool value = false; }; template<typename T> struct Resolve<T, T::my_value> { static const bool value = true; }; The goal is to specialize, the classes which have static const bool my_value = true; defined

Explain C++ SFINAE to a non-C++ programmer

微笑、不失礼 提交于 2019-11-26 21:25:01
What is SFINAE in C++? Can you please explain it in words understandable to a programmer who is not versed in C++? Also, what concept in a language like Python does SFINAE correspond to? Jerry Coffin Warning: this is a really long explanation, but hopefully it really explains not only what SFINAE does, but gives some idea of when and why you might use it. Okay, to explain this we probably need to back up and explain templates a bit. As we all know, Python uses what's commonly referred to as duck typing -- for example, when you invoke a function, you can pass an object X to that function as

Why do my SFINAE expressions no longer work with GCC 8.2?

北城余情 提交于 2019-11-26 20:38:53
问题 I recently upgraded GCC to 8.2, and most of my SFINAE expressions have stopped working. The following is somewhat simplified, but demonstrates the problem: #include <iostream> #include <type_traits> class Class { public: template < typename U, typename std::enable_if< std::is_const<typename std::remove_reference<U>::type>::value, int >::type... > void test() { std::cout << "Constant" << std::endl; } template < typename U, typename std::enable_if< !std::is_const<typename std::remove_reference

Why does enable_if_t in template arguments complains about redefinitions?

試著忘記壹切 提交于 2019-11-26 20:33:44
问题 I have the following case that works using std::enable_if : template<typename T, typename std::enable_if<std::is_same<int, T>::value>::type* = nullptr> void f() { } template<typename T, typename std::enable_if<std::is_same<double, T>::value>::type* = nullptr> void f() { } Now, I saw in cppreference the new syntax, much cleaner in my opinion : typename = std::enable_if_t<std::is_same<int, T>::value>> I wanted to port my code : template<typename T, typename = std::enable_if_t<std::is_same<int,