function-templates

Most terse and reusable way of wrapping template or overloaded functions in function objects

≡放荡痞女 提交于 2021-01-28 21:31:47
问题 Scenario 1: a template function pred template<typename T> bool pred(T t) { /* return a bool based on t */ } Scenario 2: a set of functions overloaded on the same name pred bool pred(A t) { /* return a bool based on t */ } bool pred(B t) { /* return a bool based on t */ } bool pred(C t) { /* return a bool based on t */ } ... Whichever of the two scenarii we're in, the bottom line is that pred does not refer to a function, and so it cannot be passed around, e.g. as a unary predicate to std:

Should this function call be ambiguous?

独自空忆成欢 提交于 2021-01-27 04:56:30
问题 I stumbled on this the other day and can't figure out which answer is correct, or whether both are acceptable. Specifically, I'm referring to the call to bar(T{}) in OtherFunction. From what I've been able to test on compiler explorer, the decision seems split. msvc and icc agree that it is ambiguous while gcc and clang compile the code without issue. The function bar inside the namespace hidden becomes visible through argument-dependent lookup. Additionally, msvc/icc consider the declaration

Compiler error `assigning incompatible type within if statement` [duplicate]

空扰寡人 提交于 2021-01-03 06:30:42
问题 This question already has answers here : using std::is_same, why my function still can't work for 2 types (4 answers) Closed 3 months ago . The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here Sample code /** * @brief setValue * set value to property * @param val * value to set to

Compiler error `assigning incompatible type within if statement` [duplicate]

牧云@^-^@ 提交于 2021-01-03 06:28:45
问题 This question already has answers here : using std::is_same, why my function still can't work for 2 types (4 answers) Closed 3 months ago . The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here Sample code /** * @brief setValue * set value to property * @param val * value to set to

Compiler error `assigning incompatible type within if statement` [duplicate]

江枫思渺然 提交于 2021-01-03 06:28:15
问题 This question already has answers here : using std::is_same, why my function still can't work for 2 types (4 answers) Closed 3 months ago . The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function 'core::TypedUserProperty<int>::setValue' requested here Sample code /** * @brief setValue * set value to property * @param val * value to set to

std::abs can be used in constexpr function, but only if it's templated. Why?

谁说我不能喝 提交于 2020-12-13 03:09:19
问题 Supposedly std::abs is not constexpr in the standard (even in C++20). But in practice I found out that I can compile it as constexpr under the very peculiar condition that the function is templated. See this completely working example: template<class T> constexpr T f(const T input) { return std::abs(input); } int main() { int i = -1; int a = f(i); return 0; } The code: Compiles fine with GCC, with and without the template. It doesn't work in Clang. And in Visual Studio it compiles with the

Should I declare my function template specializations or is defining them enough?

孤者浪人 提交于 2020-05-09 07:34:08
问题 I have some classes which can be checked. The code which implements this declares a function template in a header file and specializes it in different source files: // check.h template <class T> bool check(const T& object); // class1.h struct Class1 {int mass;}; // check_class1.cpp #include "class1.h" #include "check.h" template <> bool check(const Class1& object) {return object.mass < 100;} // class2.h struct Class2 {int price;}; // check_class2.cpp #include "class2.h" #include "check.h"

Deduction failure of function call with explicit template argument list and [temp.arg.explicit]/3

送分小仙女□ 提交于 2020-02-20 07:50:59
问题 [temp.arg.explicit]/3 of the C++17 standard (final draft) says about deduction of function template arguments with explicitly specified template argument lists: In contexts where deduction is done and fails, or [...], if a template argument list is specified and it, along with any default template arguments, identifies a single function template specialization, then the template-id is an lvalue for the function template specialization. How does this apply to parameter packs? Consider template