sfinae

Limit range of type template arguments for class

狂风中的少年 提交于 2020-01-24 20:14:31
问题 How can I have this effect without the arbitrary typedefs? #include <type_traits> #include <iostream> typedef int Primary; typedef float Secondary; template<Class C, std::enable_if<std::is_same<Class, Primary>::value || std::is_same<Class, Secondary>::value> = 0> class Entity { public: template<std::enable_if<std::is_same<Class, Secondary>::value>::type = 0> void onlyLegalForSecondaryEntities() { std::cout << "Works" << std::endl; } }; int main() { Entity<Secondary> e; e

Why is SFINAE causing failure when there are two functions with different signatures?

谁都会走 提交于 2020-01-23 04:10:23
问题 I was trying to wrap my head around this question here because it was written in such a way that it was hiding what it was actually doing. So I rewrote it as such: template<typename CLASS> struct has_begin { // NOTE: sig_matches() must come before fn_exists() as it is used for its // type. Also, no function bodies are needed as they are never called. // This matching sig results in a return type of true_type template<typename A_CLASS> static auto sig_matches(void(A_CLASS::*)()) -> std::true

Why void_t doesnt work in SFINAE but enable_if does

徘徊边缘 提交于 2020-01-21 02:38:45
问题 I was trying to understand how SFINAE works and I was experimenting with this code #include <type_traits> struct One { using x = int; }; struct Two { using y = int; }; template <typename T, std::void_t<typename T::x>* = nullptr> void func() {} template <typename T, std::void_t<typename T::y>* = nullptr> void func() {} /*template <typename T, std::enable_if_t<std::is_same_v<typename T::x, typename T::x>>* = nullptr> void func() {} template <typename T, std::enable_if_t<std::is_same_v<typename

SFINAE to check for inherited member functions

二次信任 提交于 2020-01-15 05:51:10
问题 Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo() , but not that B inherits one): #include <iostream> template<typename T, typename Sig> struct has_foo { template <typename U, U> struct type_check; template <typename V> static char (& chk(type_check<Sig, &V::foo>*))[1]; template <typename > static char (& chk(...))

SFINAE std::enable_if fails in visual studio 2015

青春壹個敷衍的年華 提交于 2020-01-15 02:54:04
问题 I was trying to port an open sourced project from linux to window, there are some code that compiles perfectly in linux using either g++ or clang++, but I cannot compile it under MSVC 2015, could anyone tell me how to fix it or how to get around it? I appreciate very much for your help!!! Here is the code (I have simplified it so you can focus on the key stuff) template <typename T, class IsMatch = void> class vc_hashkey { public: static constexpr bool holds_value() { return false; } };

Implementing std::variant converting constructor - or: how to find first overload of all conversions from any T to Ti from parameter pack

ε祈祈猫儿з 提交于 2020-01-12 03:27:30
问题 In the latest working draft (page 572) of the C++ standard the converting constructor of std::variant is annotated with: template <class T> constexpr variant(T&& t) noexcept(see below ); Let Tj be a type that is determined as follows: build an imaginary function FUN (Ti) for each alternative type Ti. The overload FUN (Tj) selected by overload resolution for the expression FUN (std::forward<T>(t)) defines the alternative Tj which is the type of the contained value after construction. Effects:

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

核能气质少年 提交于 2020-01-11 06:20:10
问题 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

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

孤街浪徒 提交于 2020-01-11 06:20:03
问题 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

C++98/03 std::is_constructible implementation

旧巷老猫 提交于 2020-01-10 03:53:05
问题 The base components of my hobby library has to work with C++98 and C++11 compilers. To learn and to enjoy myself I created the C++98 implementations of several type support functionality (like enable_if , conditional , is_same , is_integral etc. ...) in order to use them when there is no C++11 support. However while I was implementing is_constructible I got stuck. Is there any kind of template magic (some kind of SFINAE) with which I can implement it without C++11 support ( declval )? Of

SFINAE/enable_if based on the contents of a string parameter?

∥☆過路亽.° 提交于 2020-01-04 05:57:27
问题 I can not get my head around the following problem. I don't even really know how I could approach it. Consider this code: struct fragment_shader { std::string mPath; }; struct vertex_shader { std::string mPath; }; template <typename T> T shader(std::string path) { return T{ path }; } To create the different structs, I can write the following: auto fragmentShader = shader<vertex_shader>("some_shader.frag"); auto vertexShader = shader<fragment_shader>("some_shader.vert"); I am wondering, if it