enable-if

What are the syntax and semantics of C++ templated code?

余生颓废 提交于 2020-01-02 12:13:38
问题 template<typename T, size_t M, size_t K, size_t N, typename std::enable_if_t<std::is_floating_point<T>::value, T> = 0> void fastor2d(){//...} I copied this line of code from cpp-reference(only the std::enable_if part, i do need T and all three of the size_t 's), because i would like to use this function only when floating_types are used on it ... it does not compile. Could somebody explain to me, why, and what it even does? While i am at it, how do you call this function afterwards? Every

Optionally supporting initializer_list construction for templates maybe wrapping containers

狂风中的少年 提交于 2020-01-02 01:15:17
问题 If I have a template that wraps a standard container, it seems I can reasonably easily delegate the initializer_list constructor: template<typename T> struct holder { T t_; holder() : t_() {} holder(std::initializer_list<typename T::value_type> values) : t_(values) {} }; So this works nicely with std::vector, for instance. int main(int argc, char* argv[]) { holder<std::vector<int>> y{1,2,3}; return EXIT_SUCCESS; } But it pretty obviously doesn't work for T as 'int', or any other type that

Why isn't std::begin/end being considered here?

强颜欢笑 提交于 2019-12-25 08:33:56
问题 I have a template free function algorithm "contains": template <typename collection_type, typename element_type, typename comparison_function_type> bool contains(const collection_type & collection, const element_type & element, comparison_function_type comparison_function) { using namespace ::std; return end(collection) != find_if(begin(collection), end(collection), [&](const element_type & candidate) { return comparison_function(candidate, element); }); } This works for the following or

Pass a Failing Template Argument

心已入冬 提交于 2019-12-25 01:04:28
问题 Dan's answer to this question: Is There Anything Like a Templatized Case-Statement takes a DefaultType template parameter. Is it possible to pass something there which will force a compile time fail? For example, given this templatized function: template <typename T, typename R = enable_if_t<is_integral<T>::value, int>> R foo(T bar) {return static_cast<R>(bar);} This code will compile fine: foo(13) But this code will fail: foo(13.0) . The reason that foo(13.0) will fail at compile time is

Boost enable_if problem

杀马特。学长 韩版系。学妹 提交于 2019-12-24 23:18:43
问题 This is more or less copy pasted from boost docs and I keep getting an error (actually alot of errors) I'm trying to make sure that a template class is only used with numbers using boost. This is an exercise in boost, rather than making a template class that only uses numbers. #include <boost/utility/enable_if.hpp> #include <boost/type_traits/is_arithmetic.hpp> using namespace boost; template <class T> class A<T, typename enable_if<is_arithmetic<T> >::type> // <-- this is line 9 { int foo; };

Unlock JButton as fields have text

百般思念 提交于 2019-12-24 22:58:13
问题 As the topic suggests I want to unlock(setEnabled(true)) my JButton register when the other fields have any text but I don't know what type of listener is this. I upload an image for clearer understanding. http://postimg.org/image/ab8alz44d/ 回答1: add a document listener for each text field. void init() { //construct text fields ... // add listeners textField1.getDocument().addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) { checkUnlock(); } public void

Unified way for checking the existence of member functions, free functions and operators

旧城冷巷雨未停 提交于 2019-12-24 15:51:55
问题 I have found several several questions on here that dealt with checking if either a member function, a free function or an operator exists for a given type. The proposed solutions solve the problem at hand but each uses a different approach. I am trying to find a way to deal with each of those problems in an identical or at least similar fashion. Checking if a Type C has a member func works: template<typename, typename T> struct has_member { static_assert( std::integral_constant<T, false>:

Overload resolution for char*, char array, and string literals using constexpr, SFINAE and/or type_traits

眉间皱痕 提交于 2019-12-23 19:17:46
问题 I have run into an interesting challenge that I have been trying to solve for hours, but after much research and many failed attempts, I find myself asking this question. I would like to write 3 overloaded functions that each take one of the following types: const char* , const char(&)[N] and string literal (e.g. "BOO") . I understand that a string literal is simply a char array, but please bear with me while I explain my approach. The two functions below are able to differentiate between the

enable_if: minimal example for void member function with no arguments

喜欢而已 提交于 2019-12-23 09:30:10
问题 I am trying to get a better understanding of std::enable_if in C++11 and have been trying to write a minimal example: a class A with a member function void foo() that has different implementations based on the type T from the class template. The below code gives the desired result, but I am not understanding it fully yet. Why does version V2 work, but not V1 ? Why is the "redundant" type U required? #include <iostream> #include <type_traits> template <typename T> class A { public: A(T x) : a_

Short Circuiting Operators in an enable_if

雨燕双飞 提交于 2019-12-22 07:59:24
问题 I want to write a templatized function which takes either an array<int, 3> or an int[3] . I'm trying to capture that in an enable_if : template<typename T> enable_if_t<is_array_v<T> && extent_v<T> == 3U || !is_array_v<T> && tuple_size<T>::value == 3U> foo(const T& param) {} Unfortunately for an int[3] , tupple_size is not defined, which causes the template to fail to compile, before short circuiting is evaluated. I have also tried to do this using a conditional but that has the same problem