sfinae

(Im)perfect forwarding with variadic templates

孤者浪人 提交于 2019-11-29 00:41:25
问题 Synopsis Given a type with a variadic template constructor that forwards the arguments to an implementation class, is it possible to restrict the types being forwarded with SFINAE? Details First, consider the non-variadic case with a constructor taking a universal reference. Here one can disable forwarding of a non-const lvalue reference via SFINAE to use the copy constructor instead. struct foo { foo() = default; foo(foo const&) { std::cout << "copy" << std::endl; } template < typename T,

How to call member function only if object happens to have it? [duplicate]

久未见 提交于 2019-11-29 00:27:23
Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? I have a function f that receives a value val of type T (templated). Is there any way to call a member function on val only if the type has such a member function? Example: struct Bar { void foo() const {} }; template<class T> void f(T const& val) { // Is there any way to call *only* if foo() is available on type T? // SFINAE technique? val.foo(); } int main() { Bar bar; f(bar); f(3.14); } Sounds like the SFINAE technique to me, maybe using boost::enable_if, but I don't know how to make it work here

detecting typedef at compile time (template metaprogramming)

≯℡__Kan透↙ 提交于 2019-11-28 20:47:04
I am currently doing some template metaprogramming. In my case I can handle any "iteratable" type, i.e. any type for which a typedef foo const_iterator exists in the same manner. I was trying to use the new C++11 template metaprogramming for this, however I could not find a method to detect if a certain type is missing. Because I also need to turn on/off other template specializations based on other characteristics, I am currently using a template with two parameters, and the second one gets produced via std::enable_if . Here is what I am currently doing: template <typename T, typename Enable

SFINAE with invalid function-type or array-type parameters?

99封情书 提交于 2019-11-28 17:17:01
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main() { char c[sizeof(f<void()>(0)) == 2]; } I expected it doing SFINAE and chosing the second overload, since substitution of T into T[1] yields void [1]() Which is an invalid type, of course. Adjustment of parameter types (array->pointer) is done after substituting template parameters into function parameters and checking for valid resulting types like 14.8.2 [temp.deduct] describes. But both comeau and GCC fail to compile the above. Both with different diagnostics. Comeau says:

How to detect if a method is virtual?

眉间皱痕 提交于 2019-11-28 17:14:39
I tried to make a traits to find if a method is virtual : ( https://ideone.com/9pfaCZ ) // Several structs which should fail depending if T::f is virtual or not. template <typename T> struct Dvf : T { void f() final; }; template <typename T> struct Dvo : T { void f() override; }; template <typename T> struct Dnv : T { void f() = delete; }; template <typename U> class has_virtual_f { private: template <std::size_t N> struct helper {}; template <typename T> static std::uint8_t check(helper<sizeof(Dvf<T>)>*); template<typename T> static std::uint16_t check(...); public: static constexpr bool

Detect if a default constructor exists at compile time [duplicate]

这一生的挚爱 提交于 2019-11-28 14:30:29
This question already has an answer here: Is there a way to test whether a C++ class has a default constructor (other than compiler-provided type traits)? 7 answers I'm trying to check if a default constructor exists for a template argument. I want to do something like this: template <typename A> class Blah { Blah() { A* = new A(); } } But i want to detect at compile time via SFINAE or some other trick if that constructor exists, and raise a static_assert of my own if it doesn't. The problem arises when i have classes (like std::vector ) that dont have a "default constructor" but a constructor

SFINAE To detect non-member function existence

一曲冷凌霜 提交于 2019-11-28 14:04:25
Does anybody know of a method for specializing a template depending on whether a non-member method is defined? I know there are numerous ways for specializing if a member function exists, but I've never seen a non-member example. The specific problem is specializing the operator<< for shared_ptr to apply the operator<< if the operator<< is defined for T, and printing the mere pointer location otherwise. It would be great if all classes defined operator<< as a member, but unfortunately many use free functions. I'm imagining something like the following: template <typename T> typename enable_if<

C++11: SFINAE in template parameters, GCC vs Clang [duplicate]

痞子三分冷 提交于 2019-11-28 13:38:20
This question already has an answer here: Is there a compiler bug exposed by my implementation of an is_complete type trait? 1 answer I want to implement a little trait-class to determine if a type has overloaded operator() properly, so that I can query a type like so: FunctorCheck<F, void(int, char)>::value Originally, I got an idea on how to implement this from this question , but after seeing a Cppcon lecture on TMP , it dawned on me that this problem could be solved much more elegantly. This is what I came up with, and this compiles and runs flawlessly on Clang 3.5.0: template <typename ..

how can I use std::enable_if in a conversion operator?

我与影子孤独终老i 提交于 2019-11-28 12:18:49
Basically I want my range type to be implicitly convertible from Range<const char> to Range<const unsigned char> . std::enable_if seems impossible because the function takes no arguments and has no return. Whats the work around? Here is basically what I tried: template<typename T> class Range{ T* begin_; T* end_; public: Range(T* begin,T* end):begin_{begin},end_{end}{} template<int N> Range(T (&a)[N]):begin_{static_cast<T*>(&a[0])},end_{static_cast<T*>(&a[N-1])}{} T* Begin(){return begin_;} T* End(){return end_;} operator typename std::enable_if<std::is_same<T,const char>::value,Range<const

Why is the template specialization not chosen?

六月ゝ 毕业季﹏ 提交于 2019-11-28 12:00:54
I wrote the following code: #include <iostream> #include <string> #include <type_traits> template<typename, typename = void> struct is_incrementable : std::false_type {}; template<typename T> struct is_incrementable<T, decltype( ++std::declval<T&>() )> : std::true_type {}; int main() { std::cout << is_incrementable<std::string>::value << std::endl; std::cout << is_incrementable<int>::value << std::endl; } When I run it, I get 0 0 . But I expected 0 1 . Any ideas? Rakete1111 For std::string , the primary template is chosen and the specialization is considered. But decltype(++std::declval<T&>())