sfinae

Enable template only if the return expression is valid

戏子无情 提交于 2019-12-24 21:23:00
问题 I want to write a wrapper over std::ostream in this style: #include <iostream> struct OstreamWrapper { OstreamWrapper(std::ostream &out) : out(out) {} template< typename T > decltype(auto) operator<<(T &&arg) { return out << std::move< T >(arg); } std::ostream &out; }; int main() { OstreamWrapper wrap(std::cout); wrap << "Hello, world!"; // This works wrap << std::endl; // This does not work return 0; } The problem with this approach is that it does not work (for example) with std::endl ,

SFINAE not working on llvm/clang

此生再无相见时 提交于 2019-12-24 20:34:21
问题 In the following code I'm trying to call a functor with whatever it takes as its parameters, "whatever" being a limited set of options (the two here are not the only ones in my code). #include <memory> #include <iostream> template<class T> struct call_with_pointer { // last resort: T* template<class Callable> static auto call(Callable &callable, const std::shared_ptr<T> &param) -> decltype(callable(param.get())) { return callable(param.get()); } }; template<class T> struct call_with_shared :

using sfinae to select different method implementations in a clase template

允我心安 提交于 2019-12-24 17:09:11
问题 I have a simple wrapper class for a GTK GUI like this: template <class T> class LabeledEntry { string name; T var; Gtk::HBox hbox; Gtk::Label label; Gtk::Entry entry; public: LabeledEntry( string _name, T _var, Gtk::VBox* vbox): name( _name ), var(_var) { label.set_text( name.c_str() ); ostringstream os; os << var ; entry.set_text( os.str().c_str() ); hbox.add( label ); hbox.add( entry ); vbox->add( hbox); } T Get() { string valString( entry.get_text()); istringstream is( valString); is >>

Applying SFINAE pattern with universal reference

拜拜、爱过 提交于 2019-12-24 10:20:44
问题 I've got the following code: #include <utility> #include <iostream> #include <queue> #include <functional> #include <stdio.h> #include <future> class Runner { public: virtual void run() = 0; virtual ~Runner() { } }; class Command: public Runner { public: Command() { std::cout << "constructor" << std::endl; } virtual void run() { } }; #define EXTEND(T, F) , typename = typename std::enable_if<std::is_base_of<F, typename std::decay<T>::type>::value, typename std::decay<T>::type>::type #define

Static member initialization in a template class

不打扰是莪最后的温柔 提交于 2019-12-24 09:32:22
问题 I need to initialize a static bool inside of a template class and I tried to do it like this. The only difference I can see is that I have a constraint on type parameter T but this causes a compilation error, why? How can I solve this? Code is the following: template<class T, class = typename enable_if<is_any_integral<T>::value>::type> class fraction { static bool auto_reduce; // ... }; template<class T, class = typename enable_if<is_any_integral<T>::value>::type> bool fraction<T>::auto

Making sfinae works for functions with deduced return type?

六月ゝ 毕业季﹏ 提交于 2019-12-24 05:04:45
问题 Consider the following code: // -------------------------------------------------------------------------- // // Preprocessor #include <array> #include <vector> #include <utility> #include <iostream> #include <type_traits> // -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- // // Calls a function without arguments if it can be called template < class F, class... Args, class = decltype

SFINAE enable_if for variadic perfect forwarding template on reference/pointer const-ness

ぃ、小莉子 提交于 2019-12-24 03:52:07
问题 I want to create a variadic perfect-forwarding make_shared<T> wrapper, but one which is SFINAEd on whether the constructor of T takes any non-const reference/pointer arguments. The idea is to have two wrappers, called construct and construct_nonconst , where when constructing a Foo(int& r) or Foo(int* r) , one is obliged to use the latter. The purpose of this is so that when developers write classes whose constructors require non-const parameters, they can do so, but it clearly shows up at

How to use SFINAE with nullary member function? [duplicate]

核能气质少年 提交于 2019-12-24 02:33:10
问题 This question already has answers here : Selecting a member function using different enable_if conditions (5 answers) Closed 3 years ago . I have a class template Bird with a Boolean template parameter can_fly . Depending on that value, I want to enable a member function with the signature void fly(); . This is my code: #include <type_traits> template<bool can_fly> class Bird { public: template<typename void_t = typename std::enable_if<can_fly>::type> void_t fly() { /* ... */ } }; int main()

SFINAE failing with enum template parameter

£可爱£侵袭症+ 提交于 2019-12-23 20:43:41
问题 Can someone explain the following behaviour (I'm using Visual Studio 2010). header: #pragma once #include <boost\utility\enable_if.hpp> using boost::enable_if_c; enum WeekDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}; template<WeekDay DAY> typename enable_if_c< DAY==SUNDAY, bool >::type goToWork() {return false;} template<WeekDay DAY> typename enable_if_c< DAY!=SUNDAY, bool >::type goToWork() {return true;} source: bool b = goToWork<MONDAY>(); compiler this gives error

Overload resolution for char*, char array, and string literals using constexpr, SFINAE and/or type_traits

眉间皱痕 提交于 2019-12-23 19:17:46
问题 I have run into an interesting challenge that I have been trying to solve for hours, but after much research and many failed attempts, I find myself asking this question. I would like to write 3 overloaded functions that each take one of the following types: const char* , const char(&)[N] and string literal (e.g. "BOO") . I understand that a string literal is simply a char array, but please bear with me while I explain my approach. The two functions below are able to differentiate between the