overloading

Can't pass std::min to function, copy of std::min works

情到浓时终转凉″ 提交于 2021-02-08 15:48:19
问题 Passing std::min to a function does not compile. I copied the libcpp declaration of std::min into my source file and it works. What's wrong with the std version? Same happens with clang and gcc. Tested on godbolt: https://godbolt.org/g/zwRqUA #include <thread> #include <algorithm> namespace mystd { // declaration copied verbatim from std::min (libcpp 4.0) template <class _Tp> inline constexpr const _Tp& mymin(const _Tp& __a, const _Tp& __b) { return std::min(__a, __b); } } int main() { std:

Method Binding in Java

浪尽此生 提交于 2021-02-07 19:58:19
问题 When I was reading Thinking in Java (4th Edition) recently, I got a problem about method binding in Java. First let's look at two definitions from the book: Connecting a method call to a method body is called binding. All method binding in Java uses late binding unless the method is static or final. you can find those definitions in section Method-call binding from chapter Polymorphism . (page 281-282) To testify that, I wrote the following code: public class Test3{ public static void main

Method Binding in Java

偶尔善良 提交于 2021-02-07 19:57:07
问题 When I was reading Thinking in Java (4th Edition) recently, I got a problem about method binding in Java. First let's look at two definitions from the book: Connecting a method call to a method body is called binding. All method binding in Java uses late binding unless the method is static or final. you can find those definitions in section Method-call binding from chapter Polymorphism . (page 281-282) To testify that, I wrote the following code: public class Test3{ public static void main

Is it possible to overload operators for native datatypes?

左心房为你撑大大i 提交于 2021-02-07 17:27:46
问题 For example, if I try to do: a_string + an_int ... where a_string is type 'str' and an_int is type 'int', or: an_int + a_string There would be a TypeError because there is no implicit conversion of the types. I understand that if I were using my own subclasses of int and string, I would be able to overload the __add__() method in my classes to achieve this. However, out of curiosity, I would like to know: would it be possible to overload the + operator in the class definitions of int and str

Powershell How to Overload Array Index Operator?

只谈情不闲聊 提交于 2021-02-07 14:14:50
问题 In Powershell, how do you overload the indexing of an array operator? Here is what I am doing now: class ThreeArray { $myArray = @(1, 2, 3) [int] getValue ($index) { return $this.myArray[$index] } setValue ($index, $value) { $this.myArray[$index] = $value } } $myThreeArray = New-Object ThreeArray Write-Host $myThreeArray.getValue(1) # 2 $myThreeArray.setValue(2, 5) Write-Host $myThreeArray.getValue(2) # 5 And, I want to do this: $myThreeArray = New-Object ThreeArray Write-Host $myThreeArray[1

Name resolution of functions inside templates instantiated with qualified types

三世轮回 提交于 2021-02-07 12:52:24
问题 Consider the following C++ code example: namespace n { struct A {}; } struct B {}; void foo(int) {} template<typename T> void quux() { foo(T()); } void foo(n::A) {} void foo(B) {} int main() { quux<n::A>(); // Error (but works if you comment out the foo(int) declaration) quux<B>(); // Works return 0; } As indicated in the comment, the template instantiation quux<n::A>() causes a compiler error (on GCC 4.6.3): foo.cpp: In function ‘void quux() [with T = n::A]’: foo.cpp:22:16: instantiated from

Why is overloading on just one ref-qualifier not allowed?

时光怂恿深爱的人放手 提交于 2021-02-07 11:30:43
问题 Apparently, overloading on ref-qualifiers is not allowed – this code won't compile if you remove either & or && (just the tokens, not their functions): #include <iostream> struct S { void f() & { std::cout << "Lvalue" << std::endl; } void f() && { std::cout << "Rvalue" << std::endl; } }; int main() { S s; s.f(); // prints "Lvalue" S().f(); // prints "Rvalue" } In other words, if you have two functions of the same name and type, you have to define both if you define either . I assume this is

How to overload operator+

梦想的初衷 提交于 2021-02-05 09:04:27
问题 So I want to overload operator+ . Here is what I have so far, but it's still not working. How would I go in writing the syntax? Header file: private: int month; int year; int day; public: upDate(); upDate(int M, int D, int Y); void setDate(int M, int D, int Y); int getMonth(); int getDay(); int getYear(); int getDateCount(); string getMonthName(); friend upDate operator+(const upDate &lhs, const upDate &rhs); My .cpp file upDate::upDate() { month = 12; day = 12; year = 1999; } upDate::upDate

How to overload operator+

旧街凉风 提交于 2021-02-05 09:01:39
问题 So I want to overload operator+ . Here is what I have so far, but it's still not working. How would I go in writing the syntax? Header file: private: int month; int year; int day; public: upDate(); upDate(int M, int D, int Y); void setDate(int M, int D, int Y); int getMonth(); int getDay(); int getYear(); int getDateCount(); string getMonthName(); friend upDate operator+(const upDate &lhs, const upDate &rhs); My .cpp file upDate::upDate() { month = 12; day = 12; year = 1999; } upDate::upDate

How to overload operator+

穿精又带淫゛_ 提交于 2021-02-05 09:01:31
问题 So I want to overload operator+ . Here is what I have so far, but it's still not working. How would I go in writing the syntax? Header file: private: int month; int year; int day; public: upDate(); upDate(int M, int D, int Y); void setDate(int M, int D, int Y); int getMonth(); int getDay(); int getYear(); int getDateCount(); string getMonthName(); friend upDate operator+(const upDate &lhs, const upDate &rhs); My .cpp file upDate::upDate() { month = 12; day = 12; year = 1999; } upDate::upDate