enable-if

enable_if template param is lambda (with particular signature)

爷,独闯天下 提交于 2021-01-20 08:24:22
问题 I have something this: template<typename T> class Image { Image(int w, int h, T defaultVal){ for(int i=0; i<h; i++) for(int j=0; j<w; j++) pixel(j, i) = defaultVal; } template<typename F> Image(int w, int h, F initializer){ for(int i=0; i<h; i++) for(int j=0; j<w; j++) pixel(j, i) = initializer(j, i); } // ... }; My intention is to be able to instantiate an Image like this: Image<int> img0(w, h, 0); // image of zeroes Image<int> imgF(w, h, [](int j, int i){ // checkerboard image return (j/10

std::enable_if to filter out the argument with the type of char*

一笑奈何 提交于 2021-01-07 01:27:21
问题 Why char* pstr="hello"; pushArg(pstr); still invoke such template? You see there is &&(!std::is_same<char, typename std::remove_cv_t<std::remove_pointer_t<T>>>::value) already. template <typename T, typename std::enable_if<(!std::is_same<lua_CFunction, T*>::value) && std::is_pointer<T>::value && (!std::is_same<std::string*, T>::value) && (!std::is_same<char, typename std::remove_cv<std::remove_pointer<T>>>::value) && (!std::is_same<unsigned char, typename std::remove_cv<std::remove_pointer<T>

Member function template selection and SFINAE

。_饼干妹妹 提交于 2020-05-23 06:14:11
问题 I've been trying to understand the way C++ selects templates. Namely, consider the following code sample: template <typename R> class Curious { public: template <typename T, typename std::enable_if<std::is_const<T>::value, int>::type = 33> void test1() {} template <typename T, typename std::enable_if<!std::is_const<T>::value, int>::type = 33> void test1() {} template <typename T, typename = typename std::enable_if<std::is_const<T>::value>::type> void test2() {} template <typename T, typename

Problem with basic usage of std::enable_if

▼魔方 西西 提交于 2020-05-09 06:06:26
问题 I'm trying to make a simple template function that given some parameters, it outputs them with a space in between. Some of those can be elements of an enum, and in that case I want to output its integer value. This is the code I have: #include <iostream> #include <type_traits> using std::cerr; using std::endl; // Output integer value if parameter is an element of an enum template<typename T, typename = typename std::enable_if_t<std::is_enum<T>::value>> constexpr std::ostream& debug(const T& a

“Function template has already been defined” with mutually exclusive `enable_if`s

只愿长相守 提交于 2020-04-07 05:15:45
问题 MSVC produces error ("function template has already been defined") for the following code: template<typename T, typename = std::enable_if_t<std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } // note difference from above ---> ! template<typename T, typename = std::enable_if_t<!std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } I thought it would work because there are mutually exclusive sfinae conditions. Can you help me with the hole in my

“Function template has already been defined” with mutually exclusive `enable_if`s

戏子无情 提交于 2020-04-07 05:12:33
问题 MSVC produces error ("function template has already been defined") for the following code: template<typename T, typename = std::enable_if_t<std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } // note difference from above ---> ! template<typename T, typename = std::enable_if_t<!std::is_default_constructible<T>::value>> auto foo(T&& val) { return 0; } I thought it would work because there are mutually exclusive sfinae conditions. Can you help me with the hole in my

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