sfinae

Is it possible to write c++ template/macros to check whether two functions have the same signatures

我们两清 提交于 2019-11-27 08:07:42
问题 Is it possible to write c++ template/macros to check whether two functions have the same signatures (return type and arguments list) ? Here's a simple example of how I want to use it: int foo(const std::string& s) {...} int bar(const std::string& s) {...} if (SAME_SIGNATURES(foo, bar)) { // do something useful... make Qt signal-slot connection for example... } else { // signatures mismatch.. report a problem or something... } So is it possible somehow or is it just a pipe dream ? P.S.

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

为君一笑 提交于 2019-11-27 07:49:16
问题 This question already has an answer here : Is there a compiler bug exposed by my implementation of an is_complete type trait? (1 answer) Closed 2 years ago . 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

Why is the template specialization not chosen?

北城以北 提交于 2019-11-27 06:41:52
问题 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? 回答1: For std::string , the

std::hash specialization using sfinae?

十年热恋 提交于 2019-11-27 06:10:19
问题 As an exercise I was trying to see if I could use SFINAE to create a std::hash specialization for std::pair and std::tuple when all of its template parameters are of an unsigned type. I have a little experience with them, but from what I understand the hash function needs to have already been templated with a typename Enabled = void for me to add a specialization. I'm not really sure where to go from here. Here's an attempt which doesn't work. #include <functional> #include <type_traits>

How to detect if a method is virtual?

隐身守侯 提交于 2019-11-27 05:25:02
问题 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

Can SFINAE detect private access violations?

北战南征 提交于 2019-11-27 05:15:56
I wonder whether if i test for some member of a class and the member is private what will sfinae respond? Will it error out hard or will it say ok or will it error out in the sfinae way? Matthieu M. Yes. EDIT: C++11 Standard quote from §14.8.2 [temp.deduct] 8/ If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. [ Note: Access checking is done as part of the substitution process. —end note ] This suggests to me that private can trigger an SFINAE error. Reading

boost::enable_if not in function signature

我与影子孤独终老i 提交于 2019-11-27 04:38:38
问题 This is just a question about style: I don't like the way of C++ for template metaprogramming that requires you to use the return type or add an extra dummy argument for the tricks with SFINAE. So, the idea I came up with is to put the SFINAE thing in the template arguments definition itself, like this: #include <iostream> #include <boost/type_traits/is_array.hpp> #include <boost/utility/enable_if.hpp> using namespace std; template <typename T, typename B=typename boost::enable_if< boost::is

How to decide if a template specialization exist

二次信任 提交于 2019-11-27 04:36:55
问题 I would like to check if a certain template specialization exist or not, where the general case is not defined. Given: template <typename T> struct A; // general definition not defined template <> struct A<int> {}; // specialization defined for int I would like to define a struct like this: template <typename T> struct IsDefined { static const bool value = ???; // true if A<T> exist, false if it does not }; Is there a way to do that (ideally without C++11)? Thanks 回答1: Using the fact that you

Selecting a member function using different enable_if conditions

谁说胖子不能爱 提交于 2019-11-27 04:26:24
I am trying to determine which version of a member function gets called based on the class template parameter. I have tried this: #include <iostream> #include <type_traits> template<typename T> struct Point { void MyFunction(typename std::enable_if<std::is_same<T, int>::value, T >::type* = 0) { std::cout << "T is int." << std::endl; } void MyFunction(typename std::enable_if<!std::is_same<T, int>::value, float >::type* = 0) { std::cout << "T is not int." << std::endl; } }; int main() { Point<int> intPoint; intPoint.MyFunction(); Point<float> floatPoint; floatPoint.MyFunction(); } which I

How to detect existence of a class using SFINAE?

佐手、 提交于 2019-11-27 04:13:26
Is it possible to detect if a class exists in C++ using SFINAE ? If possible then how? Suppose we have a class that is provided only by some versions of a library. I'd like to know if it is possible to use SFINAE to detect whether the class exists or not. The result of detection is arbitrary, say an enum constant which is 1 if it exists, 0 otherwise. Mike Kinghan If we ask the compiler to tell us anything about a class type T that has not even been declared we are bound to get a compilation error. There is no way around that. Therefore if we want to know whether class T "exists", where T might