sfinae

enable_if with copy constructors

笑着哭i 提交于 2019-12-01 17:27:40
问题 I am trying std::enable_if for the first time and struggling. Any guidance would be appreciated. As a toy example, here is a simple static vector class, for which I want to define a copy constructor, but the behaviour depends on the relative sizes of the vectors: just copy data into a smaller or same-sized vector copy data into a larger vector and then pad the rest with zeroes So the vector class is: template <size_t _Size> class Vector { double _data[_Size]; public: Vector() { std::fill(

C++ why does SFINAE fail with only a class template parameter?

荒凉一梦 提交于 2019-12-01 17:27:04
I'm using SFINAE in the style of this answer in order to call a generic vector object by using an appropriate member function. For example, the following code calls operator[](int) const first, and if that doesn't exist then operator()(int) const : template<int I> struct rank : rank<I-1> { static_assert(I > 0, ""); }; template<> struct rank<0> {}; template<typename VectorType> struct VectorWrapper { auto get(int i) const { return get(v, i, rank<5>()); } template<typename V, typename = std::enable_if_t<has_bracket_operator<const V>::value> > auto get(V const& v, int i, rank<2>) const { return v

Expression SFINAE to overload on type of passed function pointer

倾然丶 夕夏残阳落幕 提交于 2019-12-01 16:52:08
问题 In this example a function is passed to an implicitly instantiated function template. // Function that will be passed as argument int foo() { return 0; } // Function template to call passed function template<typename F> int call(F f) { return f(); } template<typename F, typename A> int call(F f, A a) { return f(a); } int a = call(foo); We can break this code by adding an overload for foo() . int foo(int i) { return 0; } The name " foo " is now ambiguous and the example will no longer compile.

C++ why does SFINAE fail with only a class template parameter?

六月ゝ 毕业季﹏ 提交于 2019-12-01 15:55:54
问题 I'm using SFINAE in the style of this answer in order to call a generic vector object by using an appropriate member function. For example, the following code calls operator[](int) const first, and if that doesn't exist then operator()(int) const : template<int I> struct rank : rank<I-1> { static_assert(I > 0, ""); }; template<> struct rank<0> {}; template<typename VectorType> struct VectorWrapper { auto get(int i) const { return get(v, i, rank<5>()); } template<typename V, typename = std:

template aliases and sfinae

瘦欲@ 提交于 2019-12-01 15:22:00
问题 In the case of a substitution failure involving a template alias ( e.g. a template alias on a missing member typename, as in the code snippet below), should an error be triggered ? Clang and gcc seem to disagree on this: // some types struct bar { }; struct foo { typedef void member_type; }; // template alias template<class T> using member = typename T::member_type; template<class T> void baz(... ) { } // only works for gcc, clang fails with: no type named 'member_type' // in 'bar' template

Does the standard require std::tuple_size to be SFINAE-friendly?

ぃ、小莉子 提交于 2019-12-01 14:49:02
问题 Edit append: The question title was "do Visual Studio compiler or Clang have incorrect behavior"- but that have been changed. So I add here that clang and gcc compiles it the way I intended, but VS does not. I have the following code: template<typename S, typename T, std::size_t... I> void print_tuple_like(S& s, const T& t, std::index_sequence<I...>) { void* unused[] = { &(s << std::get<I>(t))... }; } template<typename S, typename T, std::size_t N = std::tuple_size<decltype(T::children)>:

Check if a class has a pointer data member

徘徊边缘 提交于 2019-12-01 14:18:31
Is there a way to test if a class has a pointer data member? class Test { int* p; } template< typename T > foo( T bla ) { } This should not compile. because Test has a pointer data member. Test test; foo( test ) Maybe I can use a trait to disable the template? Or is my only option macros? Maybe someone knows if boost can do it? The following can work as a protection, but the member variable has to be accessible ( public ), otherwise it won't work: #include <type_traits> class Test { public: int* p; }; template< typename T > typename std::enable_if< std::is_pointer< decltype( T::p ) >::value >:

Check if a class has a pointer data member

戏子无情 提交于 2019-12-01 09:49:56
问题 Is there a way to test if a class has a pointer data member? class Test { int* p; } template< typename T > foo( T bla ) { } This should not compile. because Test has a pointer data member. Test test; foo( test ) Maybe I can use a trait to disable the template? Or is my only option macros? Maybe someone knows if boost can do it? 回答1: The following can work as a protection, but the member variable has to be accessible ( public ), otherwise it won't work: #include <type_traits> class Test {

How to specialize a template function for enum, and specific type?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 09:36:21
I currently have a function: template<typename T> bool func(T &t, int x) { // do stuff... } However I would like to have three different function bodies: T being an enum T being unsigned char Everything else I have tried this already but didn't get far. What are the correct function declarations for these three cases to work? The closest I have been able to come up with is Case 1 being: template<typename T> typename std::enable_if< std::is_enum<T>::value, bool >::type func( T &t, int x) and Case 3 being: template<typename T> typename std::enable_if< not std::is_enum<T>::value, bool >::type

Checking whether a template argument has a member function [duplicate]

只愿长相守 提交于 2019-12-01 08:59:52
Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? This is very similar to my earlier question . I want to check whether a template argument contains a member function or not. I tried this code similar to that in the accepted answer in my previous question. struct A { int member_func(); }; struct B { }; template<typename T> struct has_member_func { template<typename C> static char func(???); //what should I put in place of '???' template<typename C> static int func(...); enum{val = sizeof(func<T>(0)) == 1}; }; int main() { std::cout<< has_member