overloading

Function with default parameter as template type

坚强是说给别人听的谎言 提交于 2019-12-23 12:40:37
问题 I am trying to use a function with a default argument as a function pointer template parameter: template <void (*F)()> class A {}; void foo1(int a = 0) {} void foo2() {} int main() { //A<foo1> a1; <-- doesn't work A<foo2> a2; } The compiler error is: main.cpp:7:7: error: could not convert template argument ‘foo1’ to ‘void (*)()’ Is there specific syntax for this to work? Or a specific language limitation? Otherwise, the alternative is to have two separate functions instead of a default

Function overloading and template deduction priority

强颜欢笑 提交于 2019-12-23 12:33:10
问题 Consider the following function declaration : template<typename T> f(const T& x); // Version 1 template<typename T1, typename T2> f(const MyClass<T1, T2>& x); // Version 2 If I call f with a type with no relation with MyClass , the first version will be called. If I call f with a MyClass type (whatever the template parameters type are) then the second version will be called. But now, consider : template<typename T1, typename T2, typename T3> MyDerivedClass : public MyClass<T1, T2> {}; What

Method overloading in C# and Java

两盒软妹~` 提交于 2019-12-23 10:53:02
问题 I ran the following methods in C#. public float Add(float num1, long num2) { Console.WriteLine("method 1"); return 0; } public float Add(int num1, float num2) { Console.WriteLine("method 2"); return 0; } Here, if I call Add(1,1) , it gives ambiguity. Now let me swap position of float and long in the first method as follows: public float Add(long num1, float num2) { Console.WriteLine("method 1"); return 0; } public float Add(int num1, float num2) { Console.WriteLine("method 2"); return 0; }

Do I need to overload methods accepting const lvalue reference for rvalue references explicitly?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:48:47
问题 currently I’m playing around with rvalue reference (C++11, g++ with gnu++x0) and I want to implement move semantics in my classes, because it just feels „right“. Do I need to overload each function which normally would accept const lvalue reference to benefit from the rvalue references? Let’s say this is my example class: class Person { public: Person() = default; Person(std::string &name); Person(const Person &rhs); Person(Person &&rhs); Person& operator=(const Person &rhs); Person& operator

Is it a good practice to overload math functions in namespace std in c++

谁说胖子不能爱 提交于 2019-12-23 09:38:51
问题 I am writing a C++ class which represents an arithmetic type (a c++ wrapper around mpfr), and I'd like to support some functions found in <cmath> (I'll take std::sqrt as an example). So I have the following class: namespace ns { class MyClass { /* ... */ public: friend MyClass sqrt(const MyClass& mc); }; } And I can use it this way: MyClass c; /* ... */ MyClass d = ns::sqrt(c); MyClass e = sqrt(c); // Apparently I don't have to specify ns:: But I cannot use it this way: MyClass f = std::sqrt

Is a bad practice to Return different types when overloading a method?

你离开我真会死。 提交于 2019-12-23 08:56:47
问题 Given this example: Interface CustomersDao Function Get(ByVal Id As Integer) As Customer Function Get(ByVal Filter As Filter) As IList(Of Customer) End Interface Public Sub Main() Dim Customer As Customer = CustomersDao.Get(4) Dim Filter As New CustomersDao.Filter Filter.Category = 2 Dim Customers As IList(Of Customer) = CustomersDao.Get(Filter) End Sub Is it a bad practice to return diferent types in the same method? 回答1: I would recommend calling the second one GetAll . Right now, it isn't

C++ overloaded new[] query : What size does it take as parameter?

匆匆过客 提交于 2019-12-23 08:54:22
问题 I have overloadded operator new[] like this void * human::operator new[] (unsigned long int count){ cout << " calling new for array with size = " << count << endl ; void * temp = malloc(count) ; return temp ; } and now calling human * h = new human[14] ; say sizeof(human) = 16 , but count it prints is 232 which is 14*16 + sizeof( int * ) = 224+8 . Why is this extra space being allocated ? And where does it fall in memory ? Because when I print *h OR h[0] I get same results , so its not in

How to overload `float()` for a custom class in Python?

泄露秘密 提交于 2019-12-23 07:39:27
问题 Summary How can I overload the built-in float for my class so when I call float() on an instance of it, my custom function gets called instead of the default built-in? My Class Hi, I was coding my own Fractions class (for arbitrarily-high floating-point operation precision). It goes like this (I haven't yet finished it): class Fractions: """My custom Fractions class giving arbitarilly high precision w/ floating-point arithmetic.""" def __init__(self, num = 0, denom = 1): """Fractions(num = 0,

Cost of using params in C#

一个人想着一个人 提交于 2019-12-23 07:19:04
问题 Does anyone have advice for using the params in C# for method argument passing. I'm contemplating making overloads for the first 6 arguments and then a 7th using the params feature. My reasoning is to avoid the extra array allocation the params feature require. This is for some high performant utility methods. Any advice? Is it a waste of code to create all the overloads? 回答1: Honestly, I'm a little bothered by everyone shouting "premature optimization!" Here's why. What you say makes perfect

Can't Access My Extension Method

ぐ巨炮叔叔 提交于 2019-12-23 07:03:29
问题 Looking for a way to check if an string contains in another ignoring upper/lower case, I found it: Works fine. Then, I tried put it to my StringExtensions namespace. namespace StringExtensions { public static class StringExtensionsClass { //... public static bool Contains(this string target, string toCheck, StringComparison comp) { return target.IndexOf(toCheck, comp) >= 0; } } } and then: using StringExtensions; ... if (".. a".Contains("A", StringComparison.OrdinalIgnoreCase)) but I get the